【问题标题】:Tkinter multiple Buttons - different font sizesTkinter 多个按钮 - 不同的字体大小
【发布时间】:2017-02-18 20:17:27
【问题描述】:

我有一个关于 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()

【问题讨论】:

    标签: python tkinter


    【解决方案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。您只需要调用其中一个,而不是两个都调用。当您调用两个或多个几何管理器时,只有您为每个小部件调用的最后一个有任何效果。

    【讨论】:

      猜你喜欢
      • 2017-08-07
      • 2019-06-15
      • 2018-05-31
      • 2018-09-27
      • 2012-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-21
      相关资源
      最近更新 更多