通過設置屬性來改變Widget的外觀
常見的屬性包括text(顯示的文字) 、color(前景色和背景色)、size(寬度和高度)、 command callbacks(點擊后的回調函數)等等
1.設置屬性,有3種方式:
1)在創建Widget時,通過關鍵字參數設定
widgetclass(master, option=value, …) => widget
實例:
# coding=utf-8 from Tkinter import * def touch(): print 'button clicked' root = Tk() btn = Button(root, text="Touch me", fg="red", bg="blue", command=touch) # 指定顯示的文本,前景色,背景色,點擊后的回調函數 btn.pack() root.mainloop()
2) 在對象創建完畢后,通過類似字典的方式,以屬性為key,進行訪問修改
# coding=utf-8 from Tkinter import * def touch(): print 'button clicked' root = Tk() btn = Button(root, text="Touch me") btn["fg"] = "red" btn["bg"] = "blue" btn["command"] = touch btn.pack() root.mainloop()
3)對象創建完畢后,調用config函數
# coding=utf-8 from Tkinter import * def touch(): print 'button clicked' root = Tk() btn = Button(root, text="Touch me") btn.config(fg="red", bg="blue", command=touch) #調用config函數,設置屬性 btn.pack() root.mainloop()
2.獲取屬性以及屬性對應的值
獲取屬性: 調用Widget的keys()方法
同時獲取屬性以及對應的值: 調用Widget對應的無參config()方法
# coding=utf-8 from Tkinter import * root = Tk() btn = Button(root, text="Touch me") print btn.keys() # 只獲取Button實例屬性 print btn.config() # 獲取Button實例的屬性和值 btn.pack() root.mainloop()
輸出:
['activebackground', 'activeforeground', 'anchor', 'background', 'bd', 'bg', 'bitmap', 'borderwidth', 'command', 'compound', 'cursor', 'default', 'disabledforeground', 'fg', 'font', 'foreground', 'height', 'highlightbackground', 'highlightcolor', 'highlightthickness', 'image', 'justify', 'overrelief', 'padx', 'pady', 'relief', 'repeatdelay', 'repeatinterval', 'state', 'takefocus', 'text', 'textvariable', 'underline', 'width', 'wraplength'] {'highlightthickness': ('highlightthickness', 'highlightThickness', 'HighlightThickness', <pixel object at 02325330>, <pixel object at 02325330>), 'text': ('text', 'text', 'Text', '', 'Touch me'), 'image': ('image', 'image', 'Image', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 03045250>, 'none'), 'height': ('height', 'height', 'Height', 0, 0), 'borderwidth': ('borderwidth', 'borderWidth', 'BorderWidth', <pixel object at 023253C0>, <pixel object at 023253C0>), 'pady': ('pady', 'padY', 'Pad', <pixel object at 023254F8>, <pixel object at 023254F8>), 'padx': ('padx', 'padX', 'Pad', <pixel object at 023253D8>, <pixel object at 023253D8>), 'font': ('font', 'font', 'Font', <font object at 0230DEB8>, 'TkDefaultFont'), 'activeforeground': ('activeforeground', 'activeForeground', 'Background', <color object at 02301EE8>, 'SystemButtonText'), 'activebackground': ('activebackground', 'activeBackground', 'Foreground', <border object at 02302398>, 'SystemButtonFace'), 'underline': ('underline', 'underline', 'Underline', -1, -1), 'width': ('width', 'width', 'Width', 0, 0), 'state': ('state', 'state', 'State', <index object at 02325378>, 'normal'), 'highlightcolor': ('highlightcolor', 'highlightColor', 'HighlightColor', <color object at 02325570>, 'SystemWindowFrame'), 'textvariable': ('textvariable', 'textVariable', 'Variable', '', ''), 'overrelief': ('overrelief', 'overRelief', 'OverRelief', '', ''), 'takefocus': ('takefocus', 'takeFocus', 'TakeFocus', '', ''), 'bd': ('bd', '-borderwidth'), 'foreground': ('foreground', 'foreground', 'Foreground', <color object at 03047630>, 'SystemButtonText'), 'bg': ('bg', '-background'), 'repeatinterval': ('repeatinterval', 'repeatInterval', 'RepeatInterval', 0, 0), 'repeatdelay': ('repeatdelay', 'repeatDelay', 'RepeatDelay', 0, 0), 'background': ('background', 'background', 'Background', <border object at 022BE118>, 'SystemButtonFace'), 'fg': ('fg', '-foreground'), 'bitmap': ('bitmap', 'bitmap', 'Bitmap', '', ''), 'highlightbackground': ('highlightbackground', 'highlightBackground', 'HighlightBackground', <border object at 02325390>, 'SystemButtonFace'), 'disabledforeground': ('disabledforeground', 'disabledForeground', 'DisabledForeground', <color object at 0230DA50>, 'SystemDisabledText'), 'wraplength': ('wraplength', 'wrapLength', 'WrapLength', <pixel object at 023252D0>, <pixel object at 023252D0>), 'default': ('default', 'default', 'Default', <index object at 0230DFF0>, 'disabled'), 'cursor': ('cursor', 'cursor', 'Cursor', '', ''), 'command': ('command', 'command', 'Command', '', ''), 'relief': ('relief', 'relief', 'Relief', <index object at 02325420>, 'raised'), 'anchor': ('anchor', 'anchor', 'Anchor', <index object at 02301D68>, 'center'), 'justify': ('justify', 'justify', 'Justify', <index object at 02325348>, 'center')}
備注: keys()方法中並沒有包含name屬性,因為name屬性只能在Widget對象創建的時候指定。
因此通過btn['name'] = 'ok' 的形式指定‘name’屬性