Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

python - What are these strange options returned by the method configure?

I was trying to understand deeply the properties of the tkinter widgets, and for that I am calling the method configure of the current widget object. There are some options of which I cannot understand their purpose.

For example, suppose I have a normal Entry object, then I call the method configure or config to return the current properties, and the result is the following (I am using the function pprint of the package called in the same way to print the result):

{'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',
                    '',
                    '')}

Of course we cannot analyse each property and the corresponding tuple, but I am asking you if there's a guide that explains exactly what each part of the tuple for each properties means.

If you want an example, we could start from the property background. The tuple is the following:

('background',  
'background',
'Background',
<border object: 'systemWindowBody'>,
'systemWindowBody')

systemWindowBody is the value returned if I call the method cget, in the following way:

print(entry.cget('bg'))

Now, what about the 3 first options? Why do we have a background written in uppercase and backgrounds written in lower case. I have thought: maybe we can use both, but actually we can just use the lower case version background, otherwise we get the following error:

_tkinter.TclError: unknown option "-Background"

I would like also to know why there are 2 repeated lowercase options.

I have noticed that this pattern of elements in the tuples are also used in most of the properties, maybe all.

I am sorry for the long question, but I would really like to understand why this format.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Each option is a list of either two or five values. If two, it is an option alias and the actual option name. bg is an example, where the alias is "bg" and the actual tcl/tk option name is "-background".

For the case of five values, the five values are:

  1. The option name
  2. The name used by the option database
  3. The name of the option class used by the option database
  4. The default value of the option
  5. The currently configured value of the option

The option database has its roots in the X window system on unix-like machines. Unfortunately, there's not much documentation in the tkinter world. Fortunately, one rarely needs mess with the option database, though it can be handy to set app-wide defaults before you start creating widgets.

If you are in an interactive python interpreter and run the command help(Tkinter) (assuming you've imported Tkinter), you'll find documentation for the commands option_add, option_get, option_clear and option_readfile. The documentation makes the unfortunate assumption that you're familiar with X11 option databases.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...