如何在 window 和 python 3 tkinter/guizero 中间居中启动第二个 window?

How to center launch second window in the middle of the main window with python 3 tkinter/guizero?

我正在尝试在主 window 的中心打开第二个 window。它需要在主 window 所在的位置以及主 window 的大小上工作。我已经设置了一些测试小部件以确保当第二个 window 关闭时它启用所有主要 windows 功能。

我在用什么。

我正在尝试做的事情的例子。

我的代码。

from guizero import *

app = App(bg='#121212',title='Main window',width=575,height=550)
app.tk.resizable(False, False)

def SecondWindow_closed():
    secondWindow.destroy()
    app.enable()
    app.focus()

def System_secondWindow():
    global secondWindow
    secondWindow = Window(app,bg='#121212',title='Settings window',width=355,height=425)
    secondWindow.tk.resizable(False, False)
    About_project=Text(secondWindow,text='About this project ',align='bottom')
    About_project.text_color='white'
    secondWindow.tk.grab_set()
    secondWindow.when_closed=SecondWindow_closed
    


Settings_button = PushButton(app, text='Settings ⚙',command=System_secondWindow)
Settings_button.text_color='white'
Test_widget=TextBox(app,)
Test_widget.bg='white'


app.display()

这段代码创建了一个位于旧代码中心的新 window。问题是它使用纯 tkinter 而不是 guizero.

import tkinter as tk

def create_second_window():
    new_root = tk.Toplevel()
    new_root.update()
    x = root.winfo_rootx() + root.winfo_width()/2 - new_root.winfo_width()/2
    y = root.winfo_rooty() + root.winfo_width()/2 - new_root.winfo_height()/2

    new_root.geometry("+%i+%i" % (x, y))

root = tk.Tk()
root.geometry("500x500")

button = tk.Button(root, text="Click me", command=create_second_window)
button.pack()

root.mainloop()

已为 guizero 更新

from guizero import *

app = App(bg='#121212',title='Main window',width=575,height=550)
app.tk.resizable(False, False)

def SecondWindow_closed():
    secondWindow.destroy()
    app.enable()
    app.focus()

def System_secondWindow():
    global secondWindow
    secondWindow = Window(app,bg='#121212',title='Settings window',width=355,height=425)
    secondWindow.tk.resizable(False, False)
    About_project=Text(secondWindow,text='About this project ',align='bottom')
    About_project.text_color='white'
    
    x = app.tk.winfo_rootx() + app.tk.winfo_width()/2 - secondWindow.tk.winfo_width()/2
    y = app.tk.winfo_rooty() + app.tk.winfo_width()/2 - secondWindow.tk.winfo_height()/2

    secondWindow.tk.geometry("+%i+%i" % (x, y))

    
    
    secondWindow.tk.grab_set()
    app.disable()
    secondWindow.when_closed=SecondWindow_closed
    


Settings_button = PushButton(app, text='Settings ⚙',command=System_secondWindow)
Settings_button.text_color='white'
Test_widget=TextBox(app,)
Test_widget.bg='white'





app.display()