【问题标题】:What are these strange options returned by the method configure?方法configure返回的这些奇怪的选项是什么?
【发布时间】:2015-01-24 01:51:54
【问题描述】:

我试图深入了解 tkinter 小部件的属性,为此我调用了当前小部件对象的方法configure。有些选项我无法理解它们的目的。

比如假设我有一个普通的Entry对象,那么我调用configureconfig方法返回当前属性,结果如下(我使用的是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


    【解决方案1】:

    每个选项都是两个或五个值的列表。如果是两个,它是选项别名和实际选项名称。 bg 是一个示例,其中别名是“bg”,实际的 tcl/tk 选项名称是“-background”。

    对于五个值的情况,五个值分别是:

    1. 选项名称
    2. 选项数据库使用的名称
    3. 选项数据库使用的选项类的名称
    4. 选项的默认值
    5. 选项的当前配置值

    选项数据库起源于类 unix 机器上的 X 窗口系统。不幸的是,tkinter 世界中没有太多的文档。幸运的是,很少需要弄乱选项数据库,尽管在开始创建小部件之前设置应用程序范围的默认值会很方便。

    如果您在交互式 python 解释器中并运行命令 help(Tkinter)(假设您已导入 Tkinter),您将找到命令 option_addoption_getoption_clearoption_readfile 的文档.该文档不幸假设您熟悉 X11 选项数据库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多