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

Python 为什么Tkinter画布要求宽度和高度增加4个像素?,python,tkinter,tk,Python,Tkinter,Tk,如果我将borderwidth设置为零,结果是相同的。我找不到解释或控制这4个额外像素的设置或属性。明白了 >>> import Tkinter >>> c = Tkinter.Canvas(width=100, height=100) >>> c.winfo_reqwidth() 104 >>> c.winfo_reqheight() 104 我调试问题的方式可能对其他有需要的人也有用: c = Tkinter.Canv

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

明白了

>>> import Tkinter
>>> c = Tkinter.Canvas(width=100, height=100)
>>> c.winfo_reqwidth()
104
>>> c.winfo_reqheight()
104
我调试问题的方式可能对其他有需要的人也有用:

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

因此,在查看了完整的配置集之后,我猜测它是highlight或closeough参数。

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

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


您可以使用以下示例直观地确认此答案:。您可以看到一个较薄的浅灰色边框,当您添加
highlightthickness=0
时,它会消失。我看不出这是如何回答这个问题的。我认为提问者知道winfo_reqwidth返回的是什么,他们会问为什么它返回的不是要求的宽度和高度。
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')}
winfo_reqheight(), winfo_reqwidth().