Cribber
Cribber

Reputation: 2913

Tkinter multiple Buttons - different font sizes

I have a question concerning the Tkinter Buttons: I try to set three different buttons, with different fontsizes, but the buttons all are created having the same font size - the size of the last button I place.

How do I solve this? Multiple buttons, different font sizes? Apparently all buttons not only have the same font size, but also the same font family...

tk = Tk()
tk.wm_title("Knowledge")
frame = Frame(width=768, height=576, bg="", colormap="new")
frame.pack_propagate(0)
frame.pack()



b = Button(frame, text = "Text", compound="left", command=callback, highlightthickness=0, font = Font(family='Helvetica', size=20, weight='bold'), bd=0, bg ="white")
b.pack()
b.place(x=100, y=100)
a = Button(frame, text = "my", compound="left", command=callback, highlightthickness=0, font = Font(family='arial', size=24, weight='bold'), bd=0, bg ="white")
a.pack()
a.place(x=100, y=140)
c = Button(frame, text = "Know", compound="left", command=callback, highlightthickness=0, font = Font(family='Helvetica', size=18, weight='bold'), bd=0, bg ="white")
c.pack()
c.place(x=100, y=180)


tk.mainloop()

Upvotes: 0

Views: 409

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385920

The fonts are getting destroyed by the garbage collector. Save the fonts to a variable before using them.

f1 = Font(family='Helvetica', size=20, weight='bold')
f2 = Font(family='arial', size=24, weight='bold')
f3 = Font(family='Helvetica', size=18, weight='bold')

b = Button(..., font = f1, ...)
a = Button(..., font = f2, ...)
c = Button(..., font = f3, ...)

Also, calling pack is pointless since you're immediately calling place right after. You only need to call one or the other, not both. When you call two or more geometry managers, only the last one you call for each widget has any effect.

Upvotes: 1

Related Questions