I am trying to get a better understanding of using fonts in tkinter and ttk.
My plan is to have two different styles for headings, each with their own font size. I used nametofont() to create an instance of the font and then set the size in two different styles:
labelFont = tkinter.font.nametofont('TkTextFont')
labelFont.config(weight='bold')
ttk.Style().configure("TLabel", font=labelFont, size=12)
ttk.Style().configure("heading.TLabel", font=labelFont, size=48)
then apply the styles to headings:
heading = ttk.Label(root, text="Heading", style="heading.TLabel")
label = ttk.Label(root, text="Label", style="TLabel") # is style redundant?
Unfortunately, I don’t get two different sizes, so this is obviously the wrong approach.
I also tried something like this:
labelFont = tkinter.font.nametofont('TkTextFont')
headingFont = tkinter.font.nametofont('TkTextFont')
# etc
thinking that I would get two independent instance of the font, but they appear to be the same instance. If they were independent, I could have used configure() to give each of them their own font size.
I took this approach because I wanted to use the built-in named font, and use variables to maintain consistency. What is the correct approach to this?
You need to use .config(size=...) on the different instances of Font:
labelFont = tkinter.font.nametofont('TkTextFont')
labelFont.config(weight='bold', size=12)
# create a clone of labelFont using Font.copy()
headingFont = labelFont.copy()
headingFont.config(size=48)
s = ttk.Style()
s.configure('TLabel', font=labelFont) # apply to all instance of ttk.Label
s.configure('heading.TLabel', font=headingFont, size=96)
heading = ttk.Label(root, text='Heading', style='heading.TLabel')
label = ttk.Label(root, text='Label') # style='TLabel' is not necessary
Related
Is there a way to get a proper Font instance out of a tag, so I can change just the size? I know I can dump it in tk.font.Font but that seems really inefficient.
for tag in self.tag_names():
#I need `f` to be a Font instance, not just a string
f = self.tag_cget(tag, 'font')
If you used font objects to create the tags, then you can get the font name from the tag and then use nametofont to convert it to an instance of tkinter.font.Font. However, this only works if the tag has a font associated with it, and if the font is a font object rather than shorthand notation (eg: ("Helvetica", 24, "bold")).
from tkinter.font import nametofont
...
for tag_name in self.tag_names():
font_name = self.tag_cget(tag_name, "font")
if font_name:
font = nametofont(font_name)
size = int(font.cget("size"))
font.configure(size = size + delta)
One method is to premake all of the Font instances and assign the instances to the applicable tag's font option. When you want to change the sizes, loop over the stored Font instances, and change their sizes directly. Every tag that uses your Font instances will change accordingly.
I got a problem with changing the height of the Treeview.heading. I have found some answers about the dimensions of Treeview.column, but when I access Treeview.heading in the documentation, there is not a single word about changing the height of the heading dynamically when the text doesn't fit (and wrapping it) or even just hard-coding height of the heading in pixels.
I don't have to split the text to two rows, but when I just keep it that long the whole table (as it has many entries) takes up the whole screen. I want to keep it smaller, therefore I need to split longer entries.
Here is how it looks like:
I can't find any documentation to verify this but it looks like the height of the heading is determined by the heading in the first column.
Reproducing the problem
col_list = ('Name', 'Three\nLine\nHeader', 'Two\nline')
tree = Treeview(parent, columns=col_list[1:])
ix = -1
for col in col_list:
ix += 1
tree.heading(f'#{ix}', text=col)
The fix
col_list = ('Name\n\n', 'Three\nLine\nHeader', 'Two\nline')
or, if you want to make it look prettier
col_list = ('\nName\n', 'Three\nLine\nHeader', 'Two\nline')
The only problem is I haven't figured out how to centre the heading on a two line header
Edit
The newlines work if it is the top level window but not if it is a dialog. Another way of doing this is to set the style. I've got no idea why this works.
style = ttk.Style()
style.configure('Treeview.Heading', foreground='black')
you can use font size to increase the header height for sometimes;
style = ttk.Style()
style.configure('Treeview.Heading', foreground='black', background='white', font=('Arial',25),)
I need to add an IF statement, FOR and WHILE loop in my form, can someone add some kind of IF statment, FOR and WHILE loop in my code?
from tkinter import * # Ingress all components from Tkinter
mGui = Tk()
mGui.geometry('400x400') # The size of the form window
mGui.title('Registration Form',)
def response():
label3 = Label(text='Thank You!',fg='White', bg='Purple',font='none 16 bold').place(x=140,y=300) # The colours and font style and size used for the response
mlabel = Label(text='Registration Form',fg='White', bg='Purple',font='none 18 bold underline') # The colours and font style and size used for the form title
mlabel.pack()
mlabel2 = Label(text='Forename',fg='White', bg='Purple',font='times 14 bold').place(x=0,y=100) # The colours and font style and size used for the label
mlabel3 = Label(text='Surname',fg='White', bg='Purple',font='times 14 bold').place(x=0,y=150) # The colours and font style and size used for the label
mbutton = Button(text = 'Submit',command = response).place(x=150,y=250) # Location of the the button 'submit' using the x and y axis
mGui.configure(background='Green') # Background colour of the form
mEntry = Entry(bg='White').place(x=100,y=100)
mEntry = Entry(bg='White').place(x=100,y=150)
mGui.mainloop() # The code iterates
Tkinter programs are event-driven, like all GUIs that I know of.
The only place where you can execute your own code is in callbacks or timeouts.
If you want to check the contents of the entry windows, you could do that in at least two ways:
Write a callback function for the Submit button that retrieves the contents of the entry windows and validates them. You could e.g. use a messagebox telling the user that the entries are not valid.
You could also set up the entry windows to validate themselves when a key is pressed. I've used this to e.g. make the text or background of an entry window red if it contains invalid text. You could also put a tip in a Label below the entry windows, like "Both Forename and Surname must at least contain one character."
I've been producing a number of nice plots with the plotLearnerPrediction function in the mlr package for R. They look like this. From looking into the source code of the plotLearnerPrediction function it looks like the color surfaces are made with geom_tile.
A plot can for example be made by:
library(mlr)
data(iris)
#make a learner
lrn <- "classif.qda"
#make a task
my.task <- makeClassifTask(data = iris, target = "Species")
#make plot
plotLearnerPrediction(learner = lrn, task = my.task)
Now I wish to change the colors, using another red, blue and green tone to match those of some other plots that I've made for a project. for this I tried scale_fill_continuous and scale_fill_manual without any luck (Error: Discrete value supplied to continuous scale) I also wish to change the legend title and the labels for each legend entry (Which I tried giving appropriate parameters to the above scale_fill's). There's a lot of info out there on how to set the geom_tile colours when producing the plot, but I haven't found any info on how to do this post-production (i.e. in somebody else's plot object). Any help would be much appreciated.
When you look into the source code you see how the plot is generated and then you can see which scale has to be overwritten or set.
In this example it's fairly easy:
g = plotLearnerPrediction(learner = lrn, task = my.task)
library(ggplot2)
g + scale_fill_manual(values = c(setosa = "yellow", versicolor = "blue", virginica = "red"))
Note that I am using Python3 and Phoenix.
I would like to display a number (double, but that does not matter now) formatted in some way (again, no matter what that way is) within a rectangle: almost a wx.StaticText but not editable by the user. This is to display some data coming from some hardware, such as a temperature.
Is there such a widget?
I tried with using the default wx.StaticText with a style but I must have done something wrong:
hbox = wx.BoxSizer(wx.HORIZONTAL)
title = wx.StaticText(parent, label=label)
title.SetLabelMarkup("<b>{}</b>".format(label))
hbox.Add(title, border=5)
value = wx.StaticText(parent, label="3.141592", style=wx.BORDER_RAISED)
value.SetWindowStyle(wx.BORDER_SIMPLE)
hbox.Add(value, border=5)
title = wx.StaticText(parent, label="\u2103")
hbox.Add(title, border=5)
Shows this on Linux (Fedora 24, GTK):
Wouldn't using a wx.TextCtrl set to read only do the job?
Temp = wx.TextCtrl(panel1, value="3.141592", style=wx.TE_READONLY)
Temp.SetBackgroundColour('green')
The simplest solution is to just use wxStaticText with a border style (e.g. wxBORDER_SIMPLE, ...). If you don't like the appearance this results in, it's pretty simple to make your own widget drawing whatever border you desire: just create a window, define its wxEVT_PAINT handler and draw the (presumably centered) text in it and a border outside of it.