5
>>> import Tkinter
>>> c = Tkinter.Canvas(width=100, height=100)
>>> c.winfo_reqwidth()
104
>>> c.winfo_reqheight()
104

The results are the same if I set borderwidth to zero. I can't find the setting or property that explains or controls these 4 extra pixels.

ubershmekel
  • 11,864
  • 10
  • 72
  • 89

2 Answers2

9

Got it!

c = Tkinter.Canvas(width=100, height=100, highlightthickness=0)
>>> c.winfo_reqwidth()
100

The way I debugged the problem may also be useful to others in need:

import pprint
pprint.pprint(c.configure())
{'background': ('background',
                'background',
                'Background',
                'SystemButtonFace',
                'SystemButtonFace'),
 'bd': ('bd', 'borderWidth'),
 'bg': ('bg', 'background'),
 'borderwidth': ('borderwidth', 'borderWidth', 'BorderWidth', '0', '0'),
 'closeenough': ('closeenough', 'closeEnough', 'CloseEnough', '1', '1.0'),
 'confine': ('confine', 'confine', 'Confine', '1', '1'),
 'cursor': ('cursor', 'cursor', 'Cursor', '', ''),
 'height': ('height', 'height', 'Height', '7c', '100'),
 'highlightbackground': ('highlightbackground',
                         'highlightBackground',
                         'HighlightBackground',
                         'SystemButtonFace',
                         'SystemButtonFace'),
 'highlightcolor': ('highlightcolor',
                    'highlightColor',
                    'HighlightColor',
                    'SystemWindowFrame',
                    'SystemWindowFrame'),
 'highlightthickness': ('highlightthickness',
                        'highlightThickness',
                        'HighlightThickness',
                        '2',
                        '0'),
 'insertbackground': ('insertbackground',
                      'insertBackground',
                      'Foreground',
                      'SystemButtonText',
                      'SystemButtonText'),
 'insertborderwidth': ('insertborderwidth',
                       'insertBorderWidth',
                       'BorderWidth',
                       '0',
                       '0'),
 'insertofftime': ('insertofftime', 'insertOffTime', 'OffTime', '300', '300'),
 'insertontime': ('insertontime', 'insertOnTime', 'OnTime', '600', '600'),
 'insertwidth': ('insertwidth', 'insertWidth', 'InsertWidth', '2', '2'),
 'offset': ('offset', 'offset', 'Offset', '0,0', '0,0'),
 'relief': ('relief', 'relief', 'Relief', 'flat', 'flat'),
 'scrollregion': ('scrollregion', 'scrollRegion', 'ScrollRegion', '', ''),
 'selectbackground': ('selectbackground',
                      'selectBackground',
                      'Foreground',
                      'SystemHighlight',
                      'SystemHighlight'),
 'selectborderwidth': ('selectborderwidth',
                       'selectBorderWidth',
                       'BorderWidth',
                       '1',
                       '1'),
 'selectforeground': ('selectforeground',
                      'selectForeground',
                      'Background',
                      'SystemHighlightText',
                      'SystemHighlightText'),
 'state': ('state', 'state', 'State', 'normal', 'normal'),
 'takefocus': ('takefocus', 'takeFocus', 'TakeFocus', '', ''),
 'width': ('width', 'width', 'Width', '10c', '100'),
 'xscrollcommand': ('xscrollcommand',
                    'xScrollCommand',
                    'ScrollCommand',
                    '',
                    ''),
 'xscrollincrement': ('xscrollincrement',
                      'xScrollIncrement',
                      'ScrollIncrement',
                      '0',
                      '0'),
 'yscrollcommand': ('yscrollcommand',
                    'yScrollCommand',
                    'ScrollCommand',
                    '',
                    ''),
 'yscrollincrement': ('yscrollincrement',
                      'yScrollIncrement',
                      'ScrollIncrement',
                      '0',
                      '0')}

So after looking at that full set of configurations I guessed it was either the highlight or closeenough parameter.

ubershmekel
  • 11,864
  • 10
  • 72
  • 89
  • You can visually confirm this answer by using this example:[link](http://stackoverflow.com/a/11894636/1217270). You can see a thin lighter-grey border, which disappears when you add `highlightthickness=0`. – Honest Abe Aug 15 '12 at 19:40
0

Because that winfo_reqwith() and winfo_reqheight() methods doesn't return actual width and height of a widget. This is what documentation says:

winfo_reqheight(), winfo_reqwidth().

Return the "natural" height (width) for self. The natural size is the minimal size needed to display the widget's contents, including padding, borders, etc. This size is calculated by the widget itself, based on the given options. The actual widget size is then determined by the widget's geometry manager, based on this value, the size of the widget's master, and the options given to the geometry manager.

Community
  • 1
  • 1
Rostyslav Dzinko
  • 39,424
  • 5
  • 49
  • 62
  • 1
    I don't see how this answers the question. I think the questioner knows what `winfo_reqwidth` returns, they are asking _why_ it returns something other than the requested width and height. – Bryan Oakley Aug 15 '12 at 20:13