Python Tkinter通过多个窗口进行通信,并在销毁后返回值



我想制作一个带有多个tkinter窗口的程序,但遇到了一些问题。我有一个包含一个按钮的主窗口。按下该按钮后,将打开一个顶层,其中包含通过用户输入一些数据的小部件。这个顶层还包含一个按钮,一旦按下,顶层就会被破坏,引入的数据会从第一个按钮返回到事件调用的函数。主窗口被破坏,数据作为参数传递给将使用它的第三个窗口

from tkinter import *

def third_window(data):
root = Tk()
lbl = Label(root, text=data)
lbl.place(x=20,y=20)
root.mainloop()
def second_window():
def event_btn(event):
e = entry.get()
if len(e) != 0:
root.destroy()
print(e)
return e
root = Toplevel()
root.geometry("400x400+200+200")
entry = Entry(root, width=15)
entry.place(x=30,y=30)
btn = Button(root, text="Send")
btn.bind("<Button-1>", event_btn)
btn.place(x=80, y=80)
root.wait_window()
root.mainloop()
def main():
def event_btn(event):
data = second_window()
print(data)
root.destroy()
third_window(data)
root = Tk()
root.geometry("200x200+100+100")
btn = Button(root, text="Test button")
btn.bind("<Button-1>", event_btn)   
btn.place(x=50, y=50)
root.mainloop()
if __name__ == "__main__":
main()

我遇到了两个问题:顶层被破坏后,主窗口没有关闭,顶层的数据没有返回。

实现这一点的一种方法是使用顶级窗口隐藏根窗口,然后更新该窗口。这让我们可以把事情浓缩很多。

import tkinter as tk
def second_window():
root.withdraw()
top = tk.Toplevel(root)
top.geometry("400x400+200+200")
entry = tk.Entry(top, width=15)
entry.place(x=30,y=30)
def actions():
x = entry.get()
entry.destroy()
btn.destroy()
tk.Label(top, text=x).place(x=20,y=20)
btn = tk.Button(top, text="Send", command=actions)
btn.place(x=80, y=80)

if __name__ == "__main__":
root = tk.Tk()
root.geometry("200x200+100+100")
tk.Button(root, text="Test button", command=second_window).place(x=50, y=50)
root.mainloop()

就我个人而言,我更喜欢OOP方法,它使以后的管理更容易。

import tkinter as tk
class Example(tk.Tk):
def __init__(self):
super().__init__()
self.geometry("200x200+100+100")
self.btn = tk.Button(self, text="Test button", command=self.update_window)
self.btn.place(x=50, y=50)
def update_window(self):
self.geometry("400x400+200+200")
self.entry = tk.Entry(self, width=15)
self.entry.place(x=30,y=30)
self.btn.config(text="Send", command=self.actions)
self.btn.place(x=80, y=80)
def actions(self):
tk.Label(self, text=self.entry.get()).place(x=20,y=20)
self.btn.destroy()
self.entry.destroy()
if __name__ == "__main__":
Example().mainloop()

也就是说你们可能不需要使用这个地方。一旦你学会了如何正确使用grid()pack(),你就可以从中获得你需要的外观。

让它工作的一种方法是简单地撤回根窗口而不是销毁它,并使用StringVar来传递数据。

你也可以简单地用你的新布局重写根,如果你不再需要上面的内容,我会看看其他例子。

你现在面临的主要问题之一是在数据传递给根之前销毁TopLevel,但在返回调用之后销毁会忽略销毁,而且对我来说,它不喜欢将TopLevel传递给根。

我真的不明白为什么你更喜欢<bind>而不是command属性作为按钮。

from tkinter import *
def third_window(data):
top = Toplevel()
lbl = Label(top, text=data)
lbl.place(x=20,y=20)
top.wait_window()
def second_window(root, v):
def event_btn():
if len(v.get()) != 0:
top.destroy()
top = Toplevel()
top.geometry("400x400+200+200")
entry = Entry(top, textvariable = v, width=15)
entry.place(x=30,y=30)
btn = Button(top, text="Send", command = event_btn)
btn.place(x=80, y=80)
root.wait_window(top)
def main():
def event_btn():
second_window(root, v)
print(v.get())
root.withdraw()
third_window(v.get())
root = Tk()
root.geometry("200x200+100+100")
btn = Button(root, text="Test button", command = event_btn) 
btn.place(x=50, y=50)
v = StringVar()
root.mainloop()
if __name__ == "__main__":
main()

最新更新