2012-08-15 18 views
5
>>> import Tkinter 
>>> c = Tkinter.Canvas(width=100, height=100) 
>>> c.winfo_reqwidth() 
104 
>>> c.winfo_reqheight() 
104 

परिणाम अगर मैं शून्य करने के लिए BorderWidth सेट एक ही हैं। मुझे सेटिंग या संपत्ति नहीं मिल रही है जो इन 4 अतिरिक्त पिक्सेल को बताती या नियंत्रित करती है।क्यों चौड़ाई और ऊंचाई के लिए Tkinter कैनवास अनुरोध 4 अतिरिक्त पिक्सल?

उत्तर

7

मिल गया!

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 पैरामीटर था।

+0

आप इस उदाहरण का उपयोग करके इस उत्तर की पुष्टि कर सकते हैं: [link] (http://stackoverflow.com/a/11894636/1217270)। आप एक पतली लाइटर-ग्रे सीमा देख सकते हैं, जो 'हाइलाइटथिकनेस = 0' जोड़ते समय गायब हो जाता है। –

0

क्योंकि वह winfo_reqwith() और winfo_reqheight() तरीकों वास्तविक चौड़ाई और एक विजेट की ऊंचाई वापस नहीं करता है। यह वही है प्रलेखन कहते हैं:

winfo_reqheight(), winfo_reqwidth(). 

वापसी स्वयं के लिए "प्राकृतिक" ऊंचाई (चौड़ाई)। प्राकृतिक आकार न्यूनतम आकार विजेट की सामग्री को प्रदर्शन, गद्दी, बॉर्डर, आदि सहित करने के लिए आवश्यक है। यह आकार, विजेट अपने आप में गणना की दिए गए विकल्पों पर आधारित है। वास्तविक विजेट आकार को विजेट के ज्यामिति प्रबंधक द्वारा निर्धारित किया जाता है, इस मान के आधार पर, विजेट के मास्टर का आकार, और ज्यामिति प्रबंधक को दिए गए विकल्प।

+1

मुझे नहीं पता कि यह सवाल का जवाब कैसे देता है। मुझे लगता है कि प्रश्नकर्ता जानता है कि 'winfo_reqwidth' रिटर्न क्या है, वे पूछ रहे हैं _why_ यह अनुरोधित चौड़ाई और ऊंचाई के अलावा कुछ और देता है। –