Using Tkinter and OOP to create transition frames in different classes (Python) - python

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))

Related

How to save shared data in tkinter for python?

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()

How do you change the variable in a function from a different class and call it back?

I am making a game with levels and in each level, I will need to be using different operators and/or different ranges. My problem is that I don't know how to change the variables in a function from a different class. I would like to do this so I don't need to copy and paste my code making it lengthy. I'd like to use self.Answer and self.strQuestion for mulitple scope.
The code below is just to make the classes functional.
from tkinter import *
import tkinter as tk
import random
from Tkinter import messagebox
class BattleMaths(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
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, levelone, leveltwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
lvl1_button = Button(self, text="LEVEL 1", command=lambda: controller.show_frame(levelone))
lvl1_button.place(relx=0.5, rely=0.5, anchor='center')
I want to put the questions def into class leveltwo while changing it to self.Answer = int(numOne) * int(numTwo) and self.strQuestion = "{} x {}".format(str(numOne), str(numTwo))
class levelone(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
def widgets(self):
#widgets here
def question(self):
self.UserAnswer = ''
numOne = random.randrange(1,10)
numTwo = random.randrange(1,10)
self.Answer = int(numOne) + int(numTwo) #change this
self.strQuestion = "{} + {}".format(str(numOne), str(numTwo)) #and change this
def answer(self):
#answer checker
class leveltwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
#question def here
root = BattleMaths()
root.title("Battle Maths")
root.geometry("400x250")
root.resizable(0,0)
root.mainloop()
Create the variables you want in the main class (BattleMaths), then you can alter them in the child classes via controller.my_variable.
Example: self.Answer created in BattleMaths and accessed in levelone via controller.Answer

Tkinker Python3 Frame Update

I have my Tkinker app where I create more frame and based on what I click I show one or another, the problem now is that I want them to refresh with new information every time that I call them; do you know how can I do?
This is my code:
class HFlair(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(10, weight=1)
container.grid_columnconfigure(10, weight=1)
self.container=container
self.frames = {}
for F in ((StartPage, None), (PageOne, None)):
page_name = F[0].__name__
frame = F[0](parent=container, controller=self, attr=F[1])
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("StartPage")
def show_frame(self, page_name,arg=None):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
if arg:
frame.users(arg)
class StartPage(tk.Frame):
def __init__(self, parent, controller,attr=None):
tk.Frame.__init__(self, parent,attr)
self.controller = controller
self.title=tk.Frame(self)
self.title.pack(side="top")
self.menu=tk.Frame(self)
self.menu.pack(side="top")
self.app=tk.Frame(self) #### frame that I want refresh
self.app.pack(side="top") #### frame that I want refresh
class PageOne(tk.Frame):
def __init__(self, parent, controller, attr=None):
self.controller=controller
tk.Frame.__init__(self, parent,attr)
self.controller = controller
self.title=tk.Frame(self)
self.title.pack(side="top")
self.menu=tk.Frame(self)
self.menu.pack(side="top")
self.app=tk.Frame(self) #### frame that I want refresh
self.app.pack(side="top") #### frame that I want refresh
My problem is that in the frame self.app I have some information that I take from the DB so every time that I go to this specific page I want the Frame refresh to take update information.
Is there some easy way to do it?
I try with .update() but didn't work, more than other didn't give me any issue but the Frame didn't refresh.

Python Tkinter, modify Text from outside the class

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")

'module' object has no attribute 'TK'

I'm a beginner of learning GUI.
My python version is 2.7 and I'm using Windows.
I've searched tkinter in folder there is only one python file which is in C:\python27.
Here is my code:
import Tkinter as tk
class Electronic_Signature_User_Program(tk.TK):
def __init__(self,*args,**kwargs):
tk.Tk.__init__(self, *args, **kwargs)
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 (Loginpage, Login_Confirm):
frame = Loginpage(container,self)
self.frames[Loginpage] = frame
frame.grid(row=0,column=0,sticky="nsew")
self.show_frame(Loginpage)
def show_frame(self,cont):
frame = self.frames[cont]
frame.tkraise()
class Loginpage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
button1 = tk.Button(self,text="Login_Confirm",command=lambda:controller.show_frame(Login_Confirm))
button1.pack()
class Login_Confirm(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
button2 = tk.Button(self,text="Loginpage",command=lambda:controller.show_frame(Loginpage))
button2.pack()
app = Electronic_Signature_User_Program()
app.title('UoL 702 Electrinic Signature User Program')
app.mainloop()
In the very first class declaration you have TK where it should be Tk.

Categories

Resources