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

时间:2017-02-18 20:17:28

标签: python tkinter

我有一个关于Tkinter按钮的问题: 我尝试设置三个不同的按钮,使用不同的字体,但创建的按钮都具有相同的字体大小 - 我放置的最后一个按钮的大小。

我该如何解决这个问题?多个按钮,不同的字体大小? 显然,所有按钮不仅具有相同的字体大小,而且还具有相同的字体系列...

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()

1 个答案:

答案 0 :(得分:1)

字体被垃圾收集器破坏了。在使用之前将字体保存到变量中。

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, ...)

此外,调用pack毫无意义,因为您马上就会立即调用place。你只需要拨打一个或另一个,而不是两个。当您调用两个或更多几何管理器时,只有您为每个窗口小部件调用的最后一个几何管理器都有效。

相关问题