为什么Tkinter画布要求宽度和高度增加4个像素?

时间:2012-08-15 18:12:21

标签: python tkinter tk

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

如果我将borderwidth设置为零,结果是相同的。我找不到解释或控制这4个额外像素的设置或属性。

2 个答案:

答案 0 :(得分:8)

知道了!

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

我调试问题的方式对其他有需要的人也很有用:

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

因此,在查看完整的配置后,我猜测它是亮点或接近参数。

答案 1 :(得分:0)

因为winfo_reqwith()winfo_reqheight()方法不返回窗口小部件的实际宽度和高度。这就是文档所说的:

winfo_reqheight(), winfo_reqwidth().
     

返回“自然”高度   (宽度)为自己。自然尺寸是所需的最小尺寸   显示小部件的内容,包括填充,边框等。这个   大小由小部件本身根据给定的选项计算。   然后,窗口小部件的几何体确定实际的窗口小部件大小   经理,基于这个值,小部件的主人的大小,以及   给几何管理器的选项。

相关问题