Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't change button font size in tkinter

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()
like image 623
user7088941 Avatar asked Mar 06 '23 09:03

user7088941


1 Answers

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()
like image 136
Mike - SMT Avatar answered Mar 20 '23 11:03

Mike - SMT