python-3.x - 如何使用 python 3 tkinter/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'
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()
解决方案
此代码创建一个位于旧窗口中心的新窗口。问题是它使用 puretkinter
而不是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()
推荐阅读
- java - Web应用程序上的Eclipse tomcat启动java.lang.ClassNotFoundException:org.mockito.Mockito
- python - 编写一个程序,计算文件中存储的所有数字的平均值
- robotics - Webots - 世界重新加载后机器人的 BoundingObject 变为空(如何防止这种情况?)
- node.js - 使用 Nodejs 将数据插入到 MongoDb
- c# - Azure Function App: TimerTrigger not Firing when Deployed
- typescript - 无法使用 CDK 为现有用户附加策略
- python - 从多个字典写入单个 JSON
- java - 元类构造函数覆盖不适用于 @CompileStatic 注释类中的方法
- android - Exoplayer2 流式传输 HLS 视频,有时仅播放有声视频(未播放视频)
- opencv - 如果我在具有恒定分辨率的相机中设置较小的分辨率会怎样?