Python Tkinter:从一个帧获取变量,以在另一个帧中显示为标签
参考 为方便起见,上述帖子的代码以合并形式在底部重复 此代码将在第PageOne帧到第PageOne帧中输入的变量传递到第PageOne帧,当按下按钮时,该变量将打印到控制台 没有成功,我一直试图设置PageTwo的标签来显示在PageOne上输入的变量。最初,我希望代码修改会像以下那样简单:Python Tkinter:从一个帧获取变量,以在另一个帧中显示为标签,python,tkinter,Python,Tkinter,参考 为方便起见,上述帖子的代码以合并形式在底部重复 此代码将在第PageOne帧到第PageOne帧中输入的变量传递到第PageOne帧,当按下按钮时,该变量将打印到控制台 没有成功,我一直试图设置PageTwo的标签来显示在PageOne上输入的变量。最初,我希望代码修改会像以下那样简单: class PageTwo(ttk.Frame): def __init__(self, parent, controller): self.controller=controll
class PageTwo(ttk.Frame):
def __init__(self, parent, controller):
self.controller=controller
ttk.Frame.__init__(self, parent)
ttk.Label(self, text='PageTwo').grid(padx=(20,20), pady=(20,20))
...
将最后一行更改为:
ttk.Label(self, text=self.controller.app_data["name"].get()).grid(padx=(20,20), pady=(20,20))
我尝试过使用不同帖子的想法进行各种组合,但似乎我缺少一些基本的推理。任何帮助都会受到这位新手的感激
从上述参考文章中整合的完整代码:
#!/usr/bin/python
from Tkinter import *
import ttk
class MyApp(Tk):
def __init__(self):
Tk.__init__(self)
container = ttk.Frame(self)
container.pack(side="top", fill="both", expand = True)
self.frames = {}
self.app_data = {"name": StringVar(),
"address": StringVar()
}
for F in (PageOne, PageTwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky = NSEW)
self.show_frame(PageOne)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class PageOne(ttk.Frame):
def __init__(self, parent, controller):
self.controller=controller
ttk.Frame.__init__(self, parent)
ttk.Label(self, text='PageOne').grid(padx=(20,20), pady=(20,20))
self.make_widget(controller)
def make_widget(self, controller):
#self.some_input = StringVar
self.some_entry = ttk.Entry(self, textvariable=self.controller.app_data["name"], width=8)
self.some_entry.grid()
button1 = ttk.Button(self, text='Next Page',
command=lambda: controller.show_frame(PageTwo))
button1.grid()
class PageTwo(ttk.Frame):
def __init__(self, parent, controller):
self.controller=controller
ttk.Frame.__init__(self, parent)
ttk.Label(self, text='PageTwo').grid(padx=(20,20), pady=(20,20))
button1 = ttk.Button(self, text='Previous Page',
command=lambda: controller.show_frame(PageOne))
button1.grid()
button2 = ttk.Button(self, text='press to print', command=self.print_it)
button2.grid()
def print_it(self):
value = self.controller.app_data["name"].get()
print "The value stored in StartPage some_entry = ",value
app = MyApp()
app.title('Multi-Page Test App')
app.mainloop()
在第一页的条目中输入任何内容之前,您可以从调用第二页的
MyApp.\uuuu init.\uuu()
为第二页创建标签
切换帧时,必须更新标签 如figbeam所述,当您切换帧时,它已经创建。当按下按钮时,我进行了编辑以更新标签
class PageTwo(ttk.Frame):
def __init__(self, parent, controller):
self.controller=controller
ttk.Frame.__init__(self, parent)
self.label = ttk.Label(self, text='PageOne')
self.label.grid(padx=(20,20), pady=(20,20))
button1 = ttk.Button(self, text='Previous Page',
command=lambda: controller.show_frame(PageOne))
button1.grid()
button2 = ttk.Button(self, text='press to print', command=self.print_it)
button2.grid()
def print_it(self):
value = self.controller.app_data["name"].get()
print ("The value stored in StartPage some_entry = %s", value)
self.label['text'] = value
或者,您可以在第二页为标签使用相同的字符串变量:
ttk.Label(self, textvariable=self.controller.app_data["name"]).grid(padx=(20,20), pady=(20,20))