【发布时间】:2012-08-12 01:37:09
【问题描述】:
>>> 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
如果我将borderwidth设置为零,结果是一样的。我找不到解释或控制这 4 个额外像素的设置或属性。
【问题讨论】:
知道了!
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')}
所以在查看了全套配置之后,我猜它要么是 highlight 要么是 closeenough 参数。
【讨论】:
highlightthickness=0 时它会消失。
因为winfo_reqwith() 和winfo_reqheight() 方法不会返回小部件的实际宽度和高度。文档是这样说的:
winfo_reqheight(), winfo_reqwidth().返回“自然”高度 (宽度)为自己。自然尺寸是所需的最小尺寸 显示小部件的内容,包括内边距、边框等。这 大小由小部件本身根据给定的选项计算。 然后,实际的小部件大小由小部件的几何形状决定 manager,基于这个值,widget 的 master 的大小,以及 为几何管理器提供的选项。
【讨论】:
winfo_reqwidth 返回什么,他们在问为什么它返回的不是请求的宽度和高度。