python tkinter在小部件创建期间隐藏窗口

python tkinter在小部件创建期间隐藏窗口,python,tkinter,Python,Tkinter,我有个小麻烦,所以我来找你,看看你能不能帮我解决。 我在Python2.7中有以下代码: # main window root = tk.Tk() root.title('My application') # create the objects in the main window w = buildWidgetClass(root) # size of the screen (monitor resolution) screenWi = root.winfo_screenwidth() sc

我有个小麻烦,所以我来找你,看看你能不能帮我解决。 我在Python2.7中有以下代码:

# main window
root = tk.Tk()
root.title('My application')
# create the objects in the main window
w = buildWidgetClass(root)
# size of the screen (monitor resolution)
screenWi = root.winfo_screenwidth()
screenHe = root.winfo_screenheight()
# now that widgets are created, find the widht and the height
root.update()
guiWi = root.winfo_width()
guiHe = root.winfo_height()
# position of the window
x = screenWi / 2 - guiWi / 2
y = screenHe / 2 - guiHe / 2
root.geometry("%dx%d+%d+%d" % (guiWi, guiHe, x, y))
< >我创建主窗口(没有任何大小),我在里面插入小部件,然后定义结果的大小,并把它放在屏幕中间。< /P> 窗口中小部件的数量可能会有所不同,因此产生的大小也会有所不同

到目前为止,一切顺利,一切顺利!我唯一的问题是窗口首先出现在屏幕的左上角,然后重新定位到中心。没什么大不了的,但也不是很专业

因此,我的想法是在小部件创建期间隐藏主窗口,然后在定义几何体后使其显示

因此,在第一行之后,我添加了:

root.withdraw()
最后:

root.update()
root.deiconify()
但是当窗口重新出现时,它没有被小部件重新调整大小,大小为1x1!! 我试图用root.iconify()替换root.draw(),窗口大小已正确调整,但令人惊讶的是,最后没有被去锥化


我对此有点迷茫…

使用
根目录。winfo_reqwidth
而不是
根目录。winfo_width
可能对您的情况有所帮助。

最后,通过kalgasnik的输入,我有了一个工作代码

# main window
root = tk.Tk()
# hide the main window during time we insert widgets
root.withdraw()

root.title('My application')
# create the objects in the main window with .grid()
w = buildWidgetClass(root)
# size of the screen (monitor resolution)
screenWi = root.winfo_screenwidth()
screenHe = root.winfo_screenheight()
# now that widgets are created, find the width and the height
root.update()
# retrieve the requested size which is different as the current size
guiWi = root.winfo_reqwidth()
guiHe = root.winfo_reqheight()
# position of the window
x = (screenWi - guiWi) / 2
y = (screenHe - guiHe) / 2
root.geometry("%dx%d+%d+%d" % (guiWi, guiHe, x, y))
# restore the window
root.deiconify()

非常感谢你们两位的时间和帮助

您使用的是什么几何体管理器
buildWidgetClass
?网格还是包?网格!为什么它很重要?不是很重要,但是
place
不会改变窗口的大小,而
pack
grid
会改变。我理解为什么我的窗口是1x1,带有“draw”。当处于“退出”状态时,窗口管理器将忽略该窗口,因此不会更新!因此,即使使用“root.update”,它的大小也是1x1,然后我用这个值定义几何体,并且它在…之后不会调整大小:-(我试图在
root.update()之前添加
root.deiconify()
找到窗口的大小,它就可以工作了……但我一开始就回来了,窗口出现在上角,然后在中间移动。必须通过ssh尝试,看看它是否真的像我想象的那么难看……不幸的是,没有!
root.winfo_reqwidth
当它应该大于600时返回200。正如我所说的,并解释在tk文档中定义,当根窗口为“撤回”时,窗口管理器将忽略它,并且从不更新其大小如果插入
root.update\u idletasks()
就在
w=buildWidgetClass(root)之前
?最后你是对的!在漫长而痛苦的测试中,我删除了
根.update()
。这就是
根.winfo_reqwidth
没有返回正确值的原因。