简体   繁体   English

Tkinter多个按钮-不同的字体大小

[英]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. 我有一个关于Tkinter按钮的问题:我尝试设置三个不同的按钮,它们的字体大小不同,但是创建的所有按钮都具有相同的字体大小-我放置的最后一个按钮的大小。

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. 另外,调用pack是没有意义的,因为您紧接着立即调用place 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. 当您调用两个或多个几何图形管理器时,只有为每个小部件调用的最后一个图形管理器才有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM