>>> import Tkinter
>>> c = Tkinter.Canvas(width=100, height=100)
>>> c.winfo_reqwidth()
104
>>> c.winfo_reqheight()
104
结果是,如果我将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
我调试问题的方法可能对其他需要的人也有用:
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')}
看完所有配置后,我猜测可能是高亮或closeenough参数的问题。
因为winfo_reqwith()
和winfo_reqheight()
方法不会返回小部件的实际宽度和高度。这就是文档所说的:
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.
winfo_reqwidth
返回什么,他们想知道为什么它会返回请求的宽度和高度之外的东西。 - Bryan Oakley