如何获取使用tkinter和python 3.6.0创建的窗口的实际几何属性?

时间:2017-09-28 01:25:42

标签: python-3.x tkinter

我创建了一个1500像素宽x 900像素高的窗口,x偏移为220像素,y偏移为120像素。创建后,我立即尝试访问窗口的宽度和高度,并获得与实际窗口不对应的值。

这是打印输出。

from tkinter import *

def callback():
    print ("Not Yet Defined")

root = Tk()
root.title("Manage My Money")

#  This segment sets the size of the window and the offsets
win_height_pixels=900
win_width_pixels=1500
win_x_offset_pixels=220
win_y_offset_pixels=120
size_string=str(win_width_pixels)+"x"+str(win_height_pixels)+"+"+str(win_x_offset_pixels)+"+"+str(win_y_offset_pixels)
root.geometry(size_string)
#   This prints out the attributes of the window
print("Right after Creation")
print ("size_string",size_string)
current_geom=root.geometry()
print ("current geometry", current_geom)
root_width=root.winfo_width()
root_reqwidth=root.winfo_reqwidth()
print ("root_width, root_reqwidth", root_width, root_reqwidth)
root_height=root.winfo_height()
root_reqheight=root.winfo_reqheight()
print ("root_height, root_reqheight", root_height, root_reqheight)
winfo_x=root.winfo_x()
winfo_y=root.winfo_y()
print("winfo_x, winfo_y", winfo_x, winfo_y)

这是代码。

[0] => Bigcommerce\Api\Resources\Category Object
        (
            [ignoreOnCreate:protected] => Array
                (
                    [0] => id
                    [1] => parent_category_list
                )

            [ignoreOnUpdate:protected] => Array
                (
                    [0] => id
                    [1] => parent_category_list
                )

            [fields:protected] => stdClass Object
                (
                    [id] => 88
                    [parent_id] => 0
                    [name] => Dell
                    [description] => 
                    [sort_order] => 0
                    [page_title] => 
                    [meta_keywords] => 
                    [meta_description] => 
                    [layout_file] => 
                    [parent_category_list] => Array
                        (
                            [0] => 88
                        )

                    [image_file] => 
                    [is_visible] => 1
                    [search_keywords] => 
                    [url] => /dell/
                )

            [id:protected] => 88
            [ignoreIfZero:protected] => Array
                (
                )

            [fieldMap:protected] => Array
                (
                )

        )

1 个答案:

答案 0 :(得分:1)

如果您在屏幕上出现窗口之前拨打winfo_width()winfo_reqwidth(),则无法获得窗口的实际大小。您必须先显示窗口。