我在Python 3.6中使用tkinter作为GUI的教程构建了一个多窗口应用程序。窗口是容器内的框架对象。现在我试着弄清楚如何将信息从一个窗口传递到另一个窗口。 到目前为止,我找到了这个解决方案:
文件main.py:
import tkinter as tk
import SecondPage
class TestApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
# Initialize Window
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
pages = (StartPage, SecondPage.SecondPage)
# Load all pages
for F in pages:
frame = F(container, self, pages)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
self.show_frame(StartPage)
# shows the desired frame
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
return True
# passes text to the window StartPage
def pass_on_text(self, text):
self.frames[StartPage].get_text(text)
# passes text to the window SecondPage
def pass_on_text2(self, text):
self.frames[SecondPage.SecondPage].get_text(text)
class StartPage(tk.Frame):
def __init__(self, parent, controller, pages):
tk.Frame.__init__(self, parent)
# define GUI elements
self.label = tk.Label(self, text="test")
button = tk.Button(self, text="2nd Page", command=lambda: controller.show_frame(pages[1]))
but_change2nd = tk.Button(self, text="Change Label on SecondPage", command=lambda: send_text("Hello, too!"))
# place GUI elements
self.label.grid(row=0, column=0)
button.grid(row=1, column=0)
but_change2nd.grid(row=2, column=0)
# send text to SecondPage
def send_text(text):
controller.pass_on_text2(text)
# get information and change the displayed text
def get_text(self, text):
self.label.config(text=text)
app = TestApp()
app.mainloop()
文件SecondPage.py:
import tkinter as tk
class SecondPage(tk.Frame):
def __init__(self, parent, controller, pages):
tk.Frame.__init__(self, parent)
self.controller = controller
# define GUI elements
self.label = tk.Label(self, text="test2")
button = tk.Button(self, text="1st Page", command=lambda: controller.show_frame(pages[0]))
but_change1st = tk.Button(self, text="Change Label on Startpage", command=lambda: send_text("Hello"))
# place GUI elements
self.label.grid(row=0, column=0)
button.grid(row=1, column=0)
but_change1st.grid(row=2, column=0)
# send text to StartPage
def send_text(text):
controller.pass_on_text(text)
# get information and change the displayed text
def get_text(self, text):
self.label.config(text=text)
这种方法很有效,但我想知道,如果有更简单的方法在SecondPage上有一个按钮来改变StartPage上的标签,反之亦然。
答案 0 :(得分:1)
在发布问题之后我已经有几天这个问题我突然想到了一个似乎有用的想法:
我将页面变量更改为实例变量:
pages = (StartPage, SecondPage.SecondPage)
到
self.pages = (StartPage, SecondPage.SecondPage)
然后我在初始化时将页面传递给每个帧:
frame = F(container, self, self.pages)
并在每个帧的__init__中添加了一个pages变量:
class StartPage(tk.Frame):
def __init__(self, parent, controller, pages):
现在,我将send_text功能更改为:
def send_text(text):
controller.frames[pages[1]].label.config(text=text)
整个main.py文件现在看起来像这样:
import tkinter as tk
import SecondPage
class TestApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
# Initialize Window
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
self.pages = (StartPage, SecondPage.SecondPage)
#self.pages = pages
# Load all pages
for F in self.pages:
frame = F(container, self, self.pages)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
self.show_frame(StartPage)
# shows the desired frame
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
return True
# passes text to the window StartPage
def pass_on_text(self, text):
self.frames[self.pages[0]].get_text(text)
# passes text to the window SecondPage
def pass_on_text2(self, text):
self.frames[self.pages[1]].get_text(text)
class StartPage(tk.Frame):
def __init__(self, parent, controller, pages):
tk.Frame.__init__(self, parent)
self.controller = controller
self.pages = pages
# define GUI elements
self.label = tk.Label(self, text="test")
button = tk.Button(self, text="2nd Page", command=lambda: controller.show_frame(pages[1]))
but_change2nd = tk.Button(self, text="Change Label on SecondPage", command=lambda: send_text("Hello, too!"))
# place GUI elements
self.label.grid(row=0, column=0)
button.grid(row=1, column=0)
but_change2nd.grid(row=2, column=0)
# send text to SecondPage
def send_text(text):
controller.frames[pages[1]].label.config(text=text)
# get information and change the displayed text
def get_text(self, text):
self.label.config(text=text)
app = TestApp()
app.mainloop()