I'm very new to the world of GUIs with Python and attempting to build my first one with multiple pages, but sharing a variable from an entry box is really throwing me through a loop. I understand there's probably a lot wrong with the code, but for now, I would really just like to better understand how to share the variables between the pages from the username entry box.
Here is the code that ties into this:(The page breaks are just where there is some unrelated code)
import tkinter as tk
from tkinter import Tk, Label, Button, StringVar
class Keep(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.shared_data ={
"email": tk.StringVar(),
"password": tk.StringVar()
}
# Skipping some code to get to the good stuff
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
# LABELS, ENTRIES, AND BUTTONS
# page break
self.entry1 = tk.Entry(self, textvariable=self.controller.shared_data["email"])
entry2 = tk.Entry(self, show = '*')
button1 = tk.Button(text="Submit", command=lambda: [controller.show_frame("PageTwo"), self.retrieve()])
# page break
def retrieve(self):
self.email = self.controller.shared_data["email"].get()
self.controller.email = self.email
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.email = self.controller.shared_data["email"].get()
label1 = tk.Label(self, text="Welcome, {}".format(self.email))
if __name__ == "__main__":
keep = Keep()
keep.mainloop()
I know the retrieve function looks pretty funky and probably not at all correct, but I've been working on this specific problem for about a week now and it has lead me down some wild rabbit holes.
The end goal is for label1 of pageTwo to display, "Welcome, (insert e-mail entered in entry1 of startPage)".
I think my issue lies with pageTwo retrieving an empty string from shared_data, but I don't understand why that is.
Any help is super appreciated!
I guess problem is because frames are created in Keep.__init__, not when you run show_frame(), so PageTwo.__init__() is executed at start and text Welcome... is create at start - before you even see StartPage.
You should create empty label in __init__ and create text Welcome... in other method (ie. update_widgets()) which you will execute after show_frame() or event inside show_frame() if all classes will have update_widgets()>
Minimal working code:
import tkinter as tk
class Keep(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.shared_data ={
"email": tk.StringVar(),
"password": tk.StringVar()
}
self.frames = {
'StartPage': StartPage(self, self),
'PageTwo': PageTwo(self, self),
}
self.current_frame = None
self.show_frame('StartPage')
def show_frame(self, name):
if self.current_frame:
self.current_frame.forget()
self.current_frame = self.frames[name]
self.current_frame.pack()
self.current_frame.update_widgets() # <-- update data in widgets
class StartPage(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.controller = controller
self.entry1 = tk.Entry(self, textvariable=self.controller.shared_data["email"])
self.entry1.pack()
entry2 = tk.Entry(self, show='*')
entry2.pack()
button = tk.Button(self, text="Submit", command=lambda:controller.show_frame("PageTwo"))
button.pack()
def update_widgets(self):
pass
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.controller = controller
self.label = tk.Label(self, text="") # <-- create empty label
self.label.pack()
def update_widgets(self):
self.label["text"] = "Welcome, {}".format(self.controller.shared_data["email"].get()) # <-- update text in label
if __name__ == "__main__":
keep = Keep()
keep.mainloop()
Related
I am creating frames for my project and so far I have it where it goes from the home page to the main page when the button is clicked. The problem I am facing is when I try to go from the home page to the log page where I am faced with an issue of calling the show_frame() function (located in MainApplication) in class MainPage.
How would I go about using arguments in MainPage so I can move from main page to log page?
class MainApplication(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# initialize frames
self.frames = {}
for F in (HomePage, MainPage, LogPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
# show home page frame first
self.show_frame(HomePage)
def show_frame(self, cont): # <-- FUNCTION HERE
frame = self.frames[cont]
frame.tkraise()
class HomePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
continue_button = ttk.Button(self, text="Enter program", width=15,
command=lambda: controller.show_frame(MainPage)) # <-- works here
continue_button.pack()
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
def success_actions(self):
self.run_script_button["text"] = "View log"
self.run_script_button.configure(
command=lambda: controller.show_frame(LogPage)) # <-- want to use here
class LogPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
pass
It works only in HomePage because you made it inside the __init__() method but in the MainPage you need it outside.
To solve this try setting controller as an instance variable:
class MainPage(tk.Frame):
def __init__(self, parent, controller):
self.controller = controller
tk.Frame.__init__(self, parent)
def success_actions(self):
self.run_script_button["text"] = "View log"
self.run_script_button.configure(
command=lambda: self.controller.show_frame(LogPage))
Pardon me for my bad grammar or explanation, since I didn't know how to explain this properly.
I try to build some gui that could switch between frame, using script from this as base Switch between two frames in tkinter.
In this case, I will have a few frame that had similar design, but different function when the button is pressed. For example, I have 2 frames that have similar 2 entries and 1 button, but the button do different command (where at sub01 frame it will multiply and at sub02 frame will divide)
This is my code:
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.grid(row=1,columnspan=4,sticky='nsew')
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (sub01, sub02):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=1,sticky="nsew")
self.choices = {'sub01','sub02'}
self.tkvar = tk.StringVar()
self.tkvar.set('sub01')
self.popMenu = tk.OptionMenu(self,self.tkvar,*self.choices)
self.popMenu.grid(row=0)
self.show_frame()
self.button1 = tk.Button(self, text="Go to Layer",command=lambda: self.show_frame())
self.button1.grid(row=0, column=1)
def show_frame(self):
'''Show a frame for the given page name'''
page_name = self.tkvar.get()
frame = self.frames[page_name]
frame.tkraise()
class sub01(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="This SubLayer 1")
label.grid(row=0)
self.entries=[]
i = 0
while i < 2:
self.entries.append(tk.Entry(self,width=10))
self.entries[i].grid(row=i+1,columnspan=2,sticky='we')
i += 1
self.btn = tk.Button(self,text="multiply", command=lambda : self.multiply())
self.btn.grid(row=i+1, columnspan=2,sticky='we')
def multiply(self):
pass
class sub02(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="This SubLayer 2")
label.grid(row=0)
self.entries=[]
i = 0
while i < 2:
self.entries.append(tk.Entry(self,width=10))
self.entries[i].grid(row=i+1,columnspan=2,sticky='w')
i += 1
self.btn = tk.Button(self,text="divide",command=lambda : self.divide())
self.btn.grid(row=i+1, columnspan=2,sticky='we')
def divide(self):
pass
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
This code itself works, but when I need to create more of these frames, it becomes inconvenient. How could I make this code simpler? Like having that similar frame as a class, and the button as other class that do differ behaviour depend of the layer shown.
Thank you in advance
The canonical way to do this sort of thing is to create a class hierarchy for your Page classes and put common functionality in the base classes and derive subclasses from them that specify the behavior that differs between them. Below is how you could do that with the sample code in your question.
Since the things that are different between them are:
The text displayed on the Label.
The text displayed on the Button.
The code in that's execute when the Button is clicked.
This means the derived classes only need to know what code to run in a generically named btn_func() method and what the text to displayed on the two widgets. The code below illustrates how to do that.
Note that I've changed the spelling of your class names to conform to the naming conventions describe in PEP 8 - Style Guide for Python Code.
import Tkinter as tk
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.grid(row=1,columnspan=4,sticky='nsew')
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (Sub01, Sub02):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=1,sticky="nsew")
self.choices = {'Sub01','Sub02'}
self.tkvar = tk.StringVar()
self.tkvar.set('Sub01')
self.popMenu = tk.OptionMenu(self,self.tkvar,*self.choices)
self.popMenu.grid(row=0)
self.show_frame()
self.button1 = tk.Button(self, text="Go to Layer",command=lambda: self.show_frame())
self.button1.grid(row=0, column=1)
def show_frame(self):
'''Show a frame for the given page name'''
page_name = self.tkvar.get()
frame = self.frames[page_name]
frame.tkraise()
class BaseSubLayer(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text=self.lbl_text)
label.grid(row=0)
self.entries=[]
i = 0
while i < 2:
self.entries.append(tk.Entry(self,width=10))
self.entries[i].grid(row=i+1,columnspan=2,sticky='we')
i += 1
self.btn = tk.Button(self,text=self.btn_func_name, command=self.btn_func)
self.btn.grid(row=i+1, columnspan=2,sticky='we')
def btn_func(self):
raise NotImplementedError
class Sub01(BaseSubLayer):
lbl_text = 'This SubLayer 1'
btn_func_name = 'multiply'
def btn_func(self):
print('Running multiply() method.')
class Sub02(BaseSubLayer):
lbl_text = 'This SubLayer 2'
btn_func_name = 'divide'
def btn_func(self):
print('Running divide() method.')
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
This is a shortened example of a longer application where I have multiple pages of widgets collecting information input by the user. The MyApp instantiates each page as a class. In the example, PageTwo would like to print the value of the StringVar which stores the data from an Entry widget in PageOne.
How do I do that? Every attempt I've tried ends up with one exception or another.
from tkinter import *
from tkinter 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 = {}
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):
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.some_input, 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):
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):
print ('The value stored in StartPage some_entry = ')#What do I put here
#to print the value of some_input from PageOne
app = MyApp()
app.title('Multi-Page Test App')
app.mainloop()
Leveraging your controller
Given that you already have the concept of a controller in place (even though you aren't using it), you can use it to communicate between pages. The first step is to save a reference to the controller in each page:
class PageOne(ttk.Frame):
def __init__(self, parent, controller):
self.controller = controller
...
class PageTwo(ttk.Frame):
def __init__(self, parent, controller):
self.controller = controller
...
Next, add a method to the controller which will return a page when given the class name or some other identifying attribute. In your case, since your pages don't have any internal name, you can just use the class name:
class MyApp(Tk):
...
def get_page(self, classname):
'''Returns an instance of a page given it's class name as a string'''
for page in self.frames.values():
if str(page.__class__.__name__) == classname:
return page
return None
note: the above implementation is based on the code in the question. The code in the question has it's origin in another answer here on stackoverflow. This code differs from the original code slightly in how it manages the pages in the controller. This uses the class reference as a key, the original answer uses the class name.
With that in place, any page can get a reference to any other page by calling that function. Then, with a reference to the page, you can access the public members of that page:
class PageTwo(ttk.Frame):
...
def print_it(self):
page_one = self.controller.get_page("PageOne")
value = page_one.some_entry.get()
print ('The value stored in StartPage some_entry = %s' % value)
Storing data in the controller
Directly accessing one page from another is not the only solution. The downside is that your pages are tightly coupled. It would be hard to make a change in one page without having to also make a corresponding change in one or more other classes.
If your pages all are designed to work together to define a single set of data, it might be wise to have that data stored in the controller, so that any given page does not need to know the internal design of the other pages. The pages are free to implement the widgets however they want, without worrying about which other pages might access those widgets.
You could, for example, have a dictionary (or database) in the controller, and each page is responsible for updating that dictionary with it's subset of data. Then, at any time you can just ask the controller for the data. In effect, the page is signing a contract, promising to keep it's subset of the global data up to date with what is in the GUI. As long as you maintain the contract, you can do whatever you want in the implementation of the page.
To do that, the controller would create the data structure before creating the pages. Since we're using tkinter, that data structure could be made up of instances of StringVar or any of the other *Var classes. It doesn't have to be, but it's convenient and easy in this simple example:
class MyApp(Tk):
def __init__(self):
...
self.app_data = {"name": StringVar(),
"address": StringVar(),
...
}
Next, you modify each page to reference the controller when creating the widgets:
class PageOne(ttk.Frame):
def __init__(self, parent, controller):
self.controller=controller
...
self.some_entry = ttk.Entry(self,
textvariable=self.controller.app_data["name"], ...)
Finally, you then access the data from the controller rather than from the page. You can throw away get_page, and print the value like this:
def print_it(self):
value = self.controller.app_data["address"].get()
...
I faced a challenge in knowing where to place the print_it function.
i added the following to make it work though I don't really understand why they are used.
def show_frame(self,page_name):
...
frame.update()
frame.event_generate("<<show_frame>>")
and added the show_frame.bind
class PageTwo(tk.Frame):
def __init__(....):
....
self.bind("<<show_frame>>", self.print_it)
...
def print_it(self,event):
...
Without the above additions, when the mainloop is executed,
Page_Two[frame[print_it()]]
the print_it function executes before PageTwo is made Visible.
try:
import tkinter as tk # python3
from tkinter import font as tkfont
except ImportError:
import Tkinter as tk #python2
import tkFont as tkfont
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title_font = tkfont.Font(family="Helvetica", size=18, weight="bold", slant="italic")
# data Dictionary
self.app_data = {"name": tk.StringVar(),
"address": tk.StringVar()}
# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the others.
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0,weight=1)
self.frames = {}
for F in (StartPage, PageOne, PageTwo):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("StartPage")
def show_frame(self, page_name):
''' Show a frame for the given page name '''
frame = self.frames[page_name]
frame.tkraise()
frame.update()
frame.event_generate("<<show_frame>>")
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="this is the start page", font=self.controller.title_font)
label.pack(side="top", fill="x", pady=10)
# Update the Name value only
self.entry1 = tk.Entry(self,text="Entry", textvariable=self.controller.app_data["name"])
self.entry1.pack()
button1 = tk.Button(self, text="go to page one", command = lambda: self.controller.show_frame("PageOne")).pack()
button2 = tk.Button(self, text="Go to page Two", command = lambda: self.controller.show_frame("PageTwo")).pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="This is page 1", font=self.controller.title_font)
label.pack(side="top", fill="x", pady=10)
# Update the Address value only
self.entry1 = tk.Entry(self,text="Entry", textvariable=self.controller.app_data["address"])
self.entry1.pack()
button = tk.Button(self, text="Go to the start page", command=lambda: self.controller.show_frame("StartPage"))
button.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
# Bind the print_it() function to this Frame so that when the Frame becomes visible print_it() is called.
self.bind("<<show_frame>>", self.print_it)
label = tk.Label(self, text="This is page 2", font=self.controller.title_font)
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, text="Go to the start page",
command=lambda: self.controller.show_frame("StartPage"))
button.pack()
def print_it(self,event):
StartPage_value = self.controller.app_data["name"].get()
print(f"The value set from StartPage is {StartPage_value}")
PageOne_value= self.controller.app_data["address"].get()
print(f"The value set from StartPage is {PageOne_value}")
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
so there is this stackoverflow question:
Switch between two frames in tkinter
Which looks like a beautiful basis for building an application. But what if on startPage i want to have multiply buttons, and display the appropriate data on PageOne? Here is my attempt:
import tkinter as tk
from functools import partial
characters = {"character1": "abc", "character2": "def", "character3": "ghi", "character4": "jkl", "character5": "mnp", "character6": "qrs" }
class AdventureGame:
''' controller for managiging frames '''
def __init__(self, master):
self.master = master
self.frames = {}
for F in (StartGame,ShowCharacters,CharacterDetail):
frame = F(parent=master, controller=self)
self.frames[F.__name__] = frame
frame.grid(row=0, column=0, sticky="nsew")
''' rais the first frame '''
self.raise_frame("StartGame")
def raise_frame(self,page_name):
''' raise a frame '''
frame = self.frames[page_name]
frame.tkraise()
def lower_frame(self,page_name):
''' lower a frame '''
frame = self.frames[page_name]
frame.lower()
class StartGame(tk.Frame):
''' introduction screen of the game '''
def __init__(self, parent, controller):
super(StartGame,self).__init__(parent)
self.controller = controller
''' introduction text '''
tk.Label(self, text="welcome to this game").grid()
tk.Button(self, text="next", command=lambda: controller.raise_frame("ShowCharacters")).grid()
class ShowCharacters(tk.Frame):
''' main frame, overview of all pokemon '''
def __init__(self, parent, controller):
super(ShowCharacters,self).__init__(parent)
self.controller = controller
for row, character in enumerate(characters):
name_label = tk.Label(self, text=characters[character])
name_label.grid(row=row,column=0)
info_button = tk.Button(self, text="view info", command=partial(self.switch,character))
info_button.grid(row=row,column=1)
def switch(self, character):
test = self.controller.raise_frame("CharacterDetail")
class CharacterDetail(tk.Frame):
''' detail view of pokemon '''
def __init__(self, parent, controller):
super(CharacterDetail,self).__init__(parent)
self.controller = controller
tk.Button(self, text="back", command=lambda: controller.lower_frame("CharacterDetail")).grid()
# somehow display characters name here
def showdetail(self):
tk.Label(self, text="test").grid()
def main():
root = tk.Tk()
app = AdventureGame(root)
root.mainloop()
main()
It feels like the the key is in the controller, as explained here:
How to get variable data from a class
It just feels like i am missing something, does someone have an idea? Or am i thinking in the wrong direction all together?
If i try to show the detail:
def switch(self, character):
test = self.controller.raise_frame("CharacterDetail")
CharacterDetail.showdetail(self)
it shows up at the ShowCharacters frame rather then the CharacterDetail frame
I guess because self is the frame, which is self from ShowCharacters
I need to do something with the controller, but my mind just goes blank
So now i pass information to the controller:
import tkinter as tk
from functools import partial
characters = {"character1": "abc", "character2": "def", "character3": "ghi", "character4": "jkl", "character5": "mnp", "character6": "qrs" }
class AdventureGame:
''' controller for managiging frames '''
def __init__(self, master):
self.master = master
self.frames = {}
for F in (StartGame,ShowCharacters,CharacterDetail):
frame = F(parent=master, controller=self)
self.frames[F.__name__] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.app_data = {"test": "test"}
''' rais the first frame '''
self.raise_frame("StartGame")
def raise_frame(self,page_name):
''' raise a frame '''
frame = self.frames[page_name]
frame.tkraise()
def lower_frame(self,page_name):
''' lower a frame '''
frame = self.frames[page_name]
frame.lower()
class StartGame(tk.Frame):
''' introduction screen of the game '''
def __init__(self, parent, controller):
super(StartGame,self).__init__(parent)
self.controller = controller
''' introduction text '''
tk.Label(self, text="welcome to this game").grid()
tk.Button(self, text="next", command=lambda: controller.raise_frame("ShowCharacters")).grid()
class ShowCharacters(tk.Frame):
''' main frame, overview of all pokemon '''
def __init__(self, parent, controller):
super(ShowCharacters,self).__init__(parent)
self.controller = controller
for row, character in enumerate(characters):
name_label = tk.Label(self, text=characters[character])
name_label.grid(row=row,column=0)
info_button = tk.Button(self, text="view info", command=partial(self.switch,character))
info_button.grid(row=row,column=1)
def switch(self, character):
test = self.controller.raise_frame("CharacterDetail")
CharacterDetail.example(self)
class CharacterDetail(tk.Frame):
''' detail view of pokemon '''
def __init__(self, parent, controller):
super(CharacterDetail,self).__init__(parent)
self.controller = controller
tk.Button(self, text="back", command=lambda: controller.lower_frame("CharacterDetail")).grid()
def example(self):
tk.Label(self, text=self.controller.app_data["test"]).grid()
def main():
root = tk.Tk()
app = AdventureGame(root)
root.mainloop()
main()
the label is still on the wrong frame, how can i get access to the right self so i can add the label to the right frame?
As you call CharacterDetail.example(self) in function switch(...) of class ShowCharacters, self will be class ShowCharacters. Therefore the label created in function example(self) of class CharacterDetail will be in ShowCharacters frame.
Try:
return the raised frame in function raise_frame(...) of class AdventureGame:
def raise_frame(self, page_name):
frame = self.frames[page_name]
frame.tkraise()
return frame # return the raised frame
modify function switch(...) of class ShowCharacters:
def switch(self, character):
frame = self.controller.raise_frame("CharacterDetail")
frame.example()
I am trying to access the Text widget defined in class FirstPage from outside of the class.
I tried to solve this problem by creating a new instance of FirstPage, but could not find the right arguments to use. Also tried to use instance of GUI to gain the access, but unsuccessfully.
My problem is solved when I can use text.insert(0.0, t) from outside of the classes. It would help me modify the text displayed with Tkinter by functions that are not directly related with the GUI.
The origin of the code I am trying to use is found: Switch between two frames in tkinter
Also I removed lines that were not necessary for this question..
import Tkinter as tk
class GUI(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.geometry(self, '580x410')
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
frame = FirstPage(container, self)
self.frames[FirstPage] = frame
frame.grid(row=0, column=0, sticky="nsew")
frame = self.frames[FirstPage]
frame.tkraise()
class FirstPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
text = tk.Text(self , height=25, width=80)
text.grid(column=0, row=0, sticky="nw")
app = GUI()
app.mainloop()
EDIT:
Here is the working code:
import Tkinter as tk
class GUI(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.geometry(self, '580x410')
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
frame = FirstPage(container, self)
self.frames[FirstPage] = frame
frame.grid(row=0, column=0, sticky="nsew")
frame = self.frames[FirstPage]
frame.tkraise()
page_name = FirstPage.__name__
self.frames[page_name] = frame
def get_page(self, page_name):
return self.frames[page_name]
class FirstPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.text = tk.Text(self , height=25, width=80)
self.text.grid(column=0, row=0, sticky="nw")
app = GUI()
app.get_page("FirstPage").text.insert("1.0", "Hello, world")
app.mainloop()
There's nothing special you need to do. As with any python object, you simply need a reference to the object in order to manipulate it.
The concept in the code you started with is to have a "controller" that controls access to all of the pages, since that object is where the pages are created. You can add a function in the controller that gives you a reference to a page, and then you can use that to call a function on that page.
Here's the changes you need to make to the controller:
class GUI(tk.Tk):
def __init__(self, *args, **kwargs):
...
page_name = FirstPage.__name__
self.frames[page_name] = frame
...
def get_page(self, page_name):
return self.frames[page_name]
You also need to modify FirstPage to keep a reference to the widget so that you can access it later:
class FirstPage(tk.Frame):
def __init__(self, parent, controller):
...
self.text = tk.Text(...)
...
From within any other code you can now access the text widget via get_page (but your pages must save a reference to the controller for this to work).
class AnotherPage(tk.Frame):
def __init__(self, parent, controller):
...
self.controller = controller
...
def some_function(self):
...
first_page = self.controller.get_page("FirstPage")
text = first_page.text.get("1.0", "end-1c")
...
first_page.text.insert("end", "some new text\n")
Note that this technique works outside of any GUI pages. In your code, app is the controller, so you can do something like this:
app = GUI()
app.get_page("FirstPage").text.insert("1.0", "Hello, world")