繁体   English   中英

如何在python Tkinter中创建小部件的单一设计?

[英]How to Create a Single Design of widgets in python Tkinter?

如您所见,字体(名称,大小,样式等)在每个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)

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

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

暂无
暂无

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

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