【问题标题】:How to Create a Single Design of widgets in python Tkinter?如何在 python Tkinter 中创建单一设计的小部件?
【发布时间】:2019-02-27 04:30:17
【问题描述】:

如您所见,字体(名称、大小、样式等)在每个Checkbutton 中重复出现。我怎样才能为他们创建一个单一的设计,而不是在每个Checkbutton 中重复相同的代码?谢谢

main.iconify()
    global motor_wire
    motor_wire = Toplevel(main)


motorframe = LabelFrame(motor_wire, text="SIZE OF WIRE", font = ('Garamond', '25', 'bold', 'underline'), padx = 270, pady = 167, bd = 8)
motorframe.place(x = 30, y = 5)
Label(motorframe).pack()

thirteen = Checkbutton(motor_wire, text = '#13',font=("Calibri", '30', 'bold'), relief = 'groove' ,
                       bd = 5,padx = 0, pady = 5).place(x = 52, y = 50)
fourteen = Checkbutton(motor_wire, text = '#14',font=("Calibri", '30', 'bold'),relief = 'groove' ,
                       bd = 5,padx = 0, pady = 5).place(x = 189, y = 50)
fifteen = Checkbutton(motor_wire, text = '#15',font=("Calibri", '30','bold'),relief = 'groove' ,
                        bd = 5, padx = 0, pady = 5).place(x = 326, y = 50)

【问题讨论】:

    标签: python tkinter widget


    【解决方案1】:

    只需创建一个重复属性的字典:

    d = dict(font=("Calibri", '30', 'bold'), relief='groove', bd=5, padx=0, pady=5)
    

    然后解压到构造函数中:

    thirteen = Checkbutton(motor_wire, text='#13', **d)
    

    记住不要链接放置方法,否则您以后将无法引用小部件:

    thirteen.place(x=52, y=50)
    

    还可以考虑为这些复选按钮使用一个列表,这样您就可以在循环中创建thirteenfourteen 等(可能也来自onezero):

    buttons = []
    for i in range(15):
        buttons.append(Checkbutton(motor_wire, text=f'#{i}', **d))
    # manual placement with .place() afterward, or maybe check out .grid()
    

    【讨论】:

      猜你喜欢
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-19
      • 1970-01-01
      • 2015-02-06
      • 2012-04-20
      • 2015-01-27
      相关资源
      最近更新 更多