0

所以我试图在Tkinter中居中窗口,但是无论出于何种原因,当我向窗口添加菜单栏时,运行代码时窗口不再居中。为什么添加菜单会导致这种情况发生,以及如何防止它发生?

如果未调用菜单功能,则窗口将正确居中。

这是代码:

from tkinter import *

def win_ctr(win):
    win.update_idletasks()
    size=tuple(int(_)for _ in win.geometry().split("+")[0].split("x"))
    win.geometry("+%d+%d" % (win.winfo_screenwidth()/2 - size[0]/2, win.winfo_screenheight()/2 - size[1]/2 ))

def ac():
    print("test")

def wmenu(win):
    menu_bar=Menu(win)
    file_menu=Menu(menu_bar,tearoff=0)
    file_menu.add_command(label="Clear",command=ac)
    file_menu.add_separator()
    file_menu.add_command(label="Quitter",command=win.quit)
    menu_bar.add_cascade(label="MENU",menu=file_menu)    
    win.config(menu=menu_bar)

def initUI(win):
    wmenu(win)

def main():
    win=Tk()
    win.title("Test")
    win.geometry("354x460")
    win.resizable(True, True)
    win.config(bg=mint)
    win_ctr(win)
    initUI(win)
    win.mainloop()
    
if __name__=="__main__":
    mint="#41B77F"
    main()
4

0 回答 0