【问题标题】:Why does the Tkinter canvas request 4 extra pixels for the width and height?为什么 Tkinter 画布要求 4 个额外像素的宽度和高度?
【发布时间】: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 个额外像素的设置或属性。

【问题讨论】:

    标签: python tkinter tk


    【解决方案1】:

    知道了!

    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 参数。

    【讨论】:

    • 您可以通过以下示例直观地确认此答案:link。您可以看到一个浅灰色的细边框,当您添加 highlightthickness=0 时它会消失。
    【解决方案2】:

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

    winfo_reqheight(), winfo_reqwidth().
    

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

    【讨论】:

    • 我不明白这是如何回答这个问题的。我认为提问者知道winfo_reqwidth 返回什么,他们在问为什么它返回的不是请求的宽度和高度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 2020-08-30
    • 1970-01-01
    • 2021-09-13
    • 2015-11-23
    • 1970-01-01
    • 2018-09-27
    相关资源
    最近更新 更多