首页 > 代码库 > Tkinter中Widget配置

Tkinter中Widget配置

通过设置属性来改变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’属性

技术分享

 

Tkinter中Widget配置