簡體   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