[英]How to center launch second window in the middle of the main window with python 3 tkinter/guizero?
我正在嘗試在主 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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.