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

字体被垃圾收集器摧毁。使用之前将字体保存到变量之前。

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。您只需要致电一个或另一个,而不是两者。当您致电两个或多个几何经理时,每个小部件只有一个呼叫的最后一个效果。

最新更新