【发布时间】:2015-01-24 01:51:54
【问题描述】:
我试图深入了解 tkinter 小部件的属性,为此我调用了当前小部件对象的方法configure。有些选项我无法理解它们的目的。
比如假设我有一个普通的Entry对象,那么我调用configure或config方法返回当前属性,结果如下(我使用的是pprint的函数以相同方式调用的包以打印结果):
{'background': ('background',
'background',
'Background',
<border object: 'systemWindowBody'>,
'systemWindowBody'),
'bd': ('bd', '-borderwidth'),
'bg': ('bg', '-background'),
'borderwidth': ('borderwidth',
'borderWidth',
'BorderWidth',
<pixel object: '2'>,
2),
'cursor': ('cursor', 'cursor', 'Cursor', <cursor object: 'xterm'>, 'xterm'),
'disabledbackground': ('disabledbackground',
'disabledBackground',
'DisabledBackground',
<border object: 'systemWindowBody'>,
'systemWindowBody'),
'disabledforeground': ('disabledforeground',
'disabledForeground',
'DisabledForeground',
<color object: '#a3a3a3'>,
'#a3a3a3'),
'exportselection': ('exportselection',
'exportSelection',
'ExportSelection',
1,
1),
'fg': ('fg', '-foreground'),
'font': ('font', 'font', 'Font', <font object: 'TkTextFont'>, 'TkTextFont'),
'foreground': ('foreground',
'foreground',
'Foreground',
<color object: 'Black'>,
'Black'),
'highlightbackground': ('highlightbackground',
'highlightBackground',
'HighlightBackground',
<color object: 'systemWindowBody'>,
'systemWindowBody'),
'highlightcolor': ('highlightcolor',
'highlightColor',
'HighlightColor',
<color object: 'Black'>,
'Black'),
'highlightthickness': ('highlightthickness',
'highlightThickness',
'HighlightThickness',
<pixel object: '3'>,
3),
'insertbackground': ('insertbackground',
'insertBackground',
'Foreground',
<border object: 'Black'>,
'Black'),
'insertborderwidth': ('insertborderwidth',
'insertBorderWidth',
'BorderWidth',
<pixel object: '0'>,
0),
'insertofftime': ('insertofftime', 'insertOffTime', 'OffTime', 300, 300),
'insertontime': ('insertontime', 'insertOnTime', 'OnTime', 600, 600),
'insertwidth': ('insertwidth',
'insertWidth',
'InsertWidth',
<pixel object: '1'>,
1),
'invalidcommand': ('invalidcommand',
'invalidCommand',
'InvalidCommand',
'',
''),
'invcmd': ('invcmd', '-invalidcommand'),
'justify': ('justify', 'justify', 'Justify', <index object: 'left'>, 'left'),
'readonlybackground': ('readonlybackground',
'readonlyBackground',
'ReadonlyBackground',
<border object: 'systemWindowBody'>,
'systemWindowBody'),
'relief': ('relief', 'relief', 'Relief', <index object: 'sunken'>, 'sunken'),
'selectbackground': ('selectbackground',
'selectBackground',
'Foreground',
<border object: 'systemHighlight'>,
'systemHighlight'),
'selectborderwidth': ('selectborderwidth',
'selectBorderWidth',
'BorderWidth',
<pixel object: '1'>,
1),
'selectforeground': ('selectforeground',
'selectForeground',
'Background',
'',
''),
'show': ('show', 'show', 'Show', '', ''),
'state': ('state', 'state', 'State', <index object: 'normal'>, 'normal'),
'takefocus': ('takefocus', 'takeFocus', 'TakeFocus', '', ''),
'textvariable': ('textvariable', 'textVariable', 'Variable', '', ''),
'validate': ('validate',
'validate',
'Validate',
<index object: 'none'>,
'none'),
'validatecommand': ('validatecommand',
'validateCommand',
'ValidateCommand',
'',
''),
'vcmd': ('vcmd', '-validatecommand'),
'width': ('width', 'width', 'Width', 20, 20),
'xscrollcommand': ('xscrollcommand',
'xScrollCommand',
'ScrollCommand',
'',
'')}
当然,我们无法分析每个属性和对应的tuple,但我想问您是否有指南可以准确解释每个属性的元组的每个部分的含义。
如果您想要一个示例,我们可以从属性background 开始。元组如下:
('background',
'background',
'Background',
<border object: 'systemWindowBody'>,
'systemWindowBody')
systemWindowBody是我调用cget方法返回的值,方式如下:
print(entry.cget('bg'))
现在,前 3 个选项怎么样?为什么我们有一个大写的背景和小写的背景。我想:也许我们可以同时使用,但实际上我们可以只使用小写版本background,否则会出现以下错误:
_tkinter.TclError: unknown option "-Background"
我也想知道为什么有2个重复的小写选项。
我注意到元组中的这种元素模式也用于大多数属性,也许是全部。
对于这个冗长的问题,我很抱歉,但我真的很想了解为什么会采用这种格式。
【问题讨论】:
标签: python python-3.x configuration tkinter