Reputation: 325
Reference is made to How to get variable data from a class
For convenience the code from the above mentioned post is repeated at the bottom in consolidated form.
This code passes a variable entered in Frame PageOne to PageTwo, and when the button is pressed, the variable is printed to the console.
Without success, I have been trying to set the label of PageTwo to show the variable entered on PageOne. Initially, I hoped that the code modification would be as simple as:
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))
...
changing the last line to:
ttk.Label(self, text=self.controller.app_data["name"].get()).grid(padx=(20,20), pady=(20,20))
I have tried various combinations using ideas from different posted, but it seems I am missing some fundamental reasoning. Any help would be very much appreciated by this newbie.
Full code consolidated from the above referenced post:
#!/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()
Upvotes: 2
Views: 2021
Reputation: 2664
As mentioned by figbeam, when you switch frame it is already created. I did an edit to update the label when the button is pressed.
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
Alternatively, you could use the same string var for your label in page two:
ttk.Label(self, textvariable=self.controller.app_data["name"]).grid(padx=(20,20), pady=(20,20))
Upvotes: 2
Reputation: 7176
You create the label for page two from MyApp.__init__()
which invokes PageTwo.__init__()
, before anything is entered into the entry on page one.
You'll have to update the label when you switch frames.
Upvotes: 2