5

I can't seem to change the size of my font in tkinter! No matter which size I choose, the button text displays the same. If I deleted the whole stlye line, it's displayed smaller.

Similarly, the font always looks the same, no matter what I choose.

I want to finetune the size and the font, can you please help me=?

import tkinter
import tkinter.ttk as ttk
from tkinter import font

root = tkinter.Tk()

frame = ttk.Frame(root)
frame.grid(column=0, row=0)

style = ttk.Style(root)

ttk.Button(frame, text="Open file", command=None).grid(column=0, row=1)

ttk.Style().configure("TButton", font=font.Font(family='wasy10', size=80)) #I can choose any value here instead of "80" and any font like "Helvetica" - nothing will change

root.mainloop()
user7088941
  • 261
  • 1
  • 4
  • 11

1 Answers1

8

You do not need to import font. ttk style has its own font argument. Just put the style in the first option and the font size in the 2nd option.

I would also use the variable name to edit the style. Instead of calling:

ttk.Style().configure()

Do this:

style.configure()

Take a look at the below.

import tkinter
import tkinter.ttk as ttk


root = tkinter.Tk()

frame = ttk.Frame(root)
frame.grid(column=0, row=0)

style = ttk.Style(root)
style.configure("TButton", font=('wasy10', 80))

ttk.Button(frame, text="Open file", command=None, style="TButton").grid(column=0, row=1)


root.mainloop()

On the advice of Bryan Oakley in the comments here is a 2nd option that is close to what you are trying to do with fort.

This option saves a referent to the font object and then uses it to update the style.

import tkinter
import tkinter.ttk as ttk
from tkinter import font


root = tkinter.Tk()

frame = ttk.Frame(root)
frame.grid(column=0, row=0)

style = ttk.Style(root)
font = font.Font(family="wasy10", size=80)
style.configure("TButton", font=font)

ttk.Button(frame, text="Open file", command=None, style="TButton").grid(column=0, row=1)

root.mainloop()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • I would argue that explicitly setting the family and size is better than using a tuple. If you create a `font.Font` object and save a reference to it, you can change it at runtime and every widget that uses it will automatically adjust to the new value. – Bryan Oakley Apr 17 '18 at 13:13
  • @BryanOakley Do you mean to save it as a reference like one would safe an image reference? – Mike - SMT Apr 17 '18 at 13:15
  • Yes. `font = font.Font(family="wasy10", size=80)`; then later you can do `font.configure(size=60)` and the widget will automatically change. You also get the benefit of having to define it in one place, rather than in every widget that needs the same font. – Bryan Oakley Apr 17 '18 at 13:16
  • @BryanOakley thanks for the advice I have added it to my answer. – Mike - SMT Apr 17 '18 at 13:22
  • You need to slightly adjust the formatting of that last block of code -- the imports seem to not be part of the code markup. – Bryan Oakley Apr 17 '18 at 13:22
  • @BryanOakley Ya I noticed that as well and fixed it right before you commented. – Mike - SMT Apr 17 '18 at 13:22
  • Here's one weird question that's bugging me: If I change just twoslines in your code, by moving `font = font.Font(family="wasy10", size=80)` inside `style.configure`, so that I have `style.configure("TButton",font= font.Font(family="wasy10", size=80))`, then that should not change anything. Yet now the font change does not have any effect anymore! What is happening ?! – user7088941 Apr 17 '18 at 18:02
  • To be honest I am not sure. It may be acting the same way an image object acts if you don't save the reference. If that is the case it might be getting garbage collected because no reference to the object was saved as a variable. But don't take that as fact as I am not 100% sure. – Mike - SMT Apr 17 '18 at 18:04