如何使用 python 3 tkinter/guizero 在主窗口中间启动第二个窗口?

问题描述

我试图在主窗口的中心打开第二个窗口。它需要在主窗口所在的位置以及主窗口的大小如何工作。我已经设置了一些测试小部件,以确保当第二个窗口关闭时,它会启用所有主窗口功能

我在用什么。

我正在尝试做的示例。

enter image description here

我的代码

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()

解决方法

此代码创建一个位于旧窗口中心的新窗口。问题在于它使用纯 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()

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...