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()
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.