簡體   English   中英

python tkinter在創建小部件期間隱藏窗口

[英]python tkinter hide the window during widget creation

我有一個小問題,所以我來找你,看看你是否可以幫忙解決它。 我在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))

所以我創建了主窗口(沒有任何大小)我在里面插入小部件,然后我定義結果的大小,並將它放在屏幕的中間。

窗口中窗口小部件的數量可能會有所不同,因此產生的大小!

到目前為止,這么好,它的作品! 我唯一的問題是窗口首先出現在屏幕的左上角,然后重新定位到中心。 沒什么大不了的,但也不是很專業。

所以我的想法是在窗口小部件創建時隱藏主窗口,然后在定義幾何體后使其出現。

所以在第一行之后,我補充道:

root.withdraw()

然后在最后:

root.update()
root.deiconify()

但是當窗口重新出現時,它沒有被小部件重新調整大小並且大小為1x1 !! 我試圖以取代root.iconify(),正確地調整大小,但令人驚訝的,是不是在年底取消圖標化窗口root.withdraw()!

我有點迷失在這......

使用root.winfo_reqwidth而不是root.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()

非常感謝你們的時間和幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM