Reputation: 5
I want to pass the value stored in self.pin
, which we get from the user through entry()
in StartPage
to the __init__
method of Options
class but only when I click the submit
button. Also this must be noted that there are multiple pages and I have used tkraise()
to raise one page over the other. Explanation will be appreciated as I'm new to tkinter and oop. My whole code is given below:
import tkinter as tk
from tkinter import font as tkfont
from tkinter import messagebox
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title_font = tkfont.Font(family='Courier New', size=18, weight="bold")
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, PageThree, PageFour, Options):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("StartPage")
def show_frame(self, page_name):
frame = self.frames[page_name]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Enter 4 digit pin", font=controller.title_font, fg="red")
label.pack(side="top", fill="x", pady=10)
self.pin = tk.Entry(self, bd = 4, relief="groove", show="*", font=20, justify='center')
# self.pin="786125"
self.pin.pack()
submit = tk.Button(self, text="Submit", width=12,font=tkfont.Font(size=12),command=lambda: [controller.show_frame("Options")], cursor="hand2")
submit.pack()
class Options(StartPage):
def __init__(self, parent, controller):
# StartPage.__init__(self, parent, controller)
# print(self.pin.get())
tk.Frame.__init__(self, parent)
self.controller = controller
f1=tk.Frame(self)
f1.pack()
f2=tk.Frame(self)
f2.pack(side="bottom")
button1 = tk.Button(f1, text="Balance Inquiry",relief="flat", padx=30, justify='left', cursor="hand2", fg="red", font=tkfont.Font(size=10),
command=lambda: controller.show_frame("PageOne"))
button1.pack(side="left")
button2 = tk.Button(f1, text="Deposit",relief="flat", padx=50, justify='right', cursor="hand2", fg="red", font=tkfont.Font(size=10),
command=lambda: controller.show_frame("PageTwo"))
button2.pack(side="left")
button3 = tk.Button(f2, text="Withdraw",relief="flat", padx=50, justify='left', cursor="hand2", fg="red", font=tkfont.Font(size=10),
command=lambda: controller.show_frame("PageThree"))
button3.pack(side="left")
button4 = tk.Button(f2, text="Pin Change",relief="flat", padx=50, justify='right', cursor="hand2", fg="red", font=tkfont.Font(size=10),
command=lambda: controller.show_frame("PageFour"))
button4.pack(side="left")
Upvotes: 0
Views: 61
Reputation: 15226
Personally I would keep the value stored in the controller and then access it from the other pages. Here is a modified version of your code that can access the stored pin.
This is done by adding a command to the buttons lambda and saving the entry value to the controller.
import tkinter as tk
from tkinter import font as tkfont
from tkinter import messagebox
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.pin_stored = ""
self.title_font = tkfont.Font(family='Courier New', size=18, weight="bold")
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, Options):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("StartPage")
def show_frame(self, page_name):
frame = self.frames[page_name]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Enter 4 digit pin", font=controller.title_font, fg="red")
label.pack(side="top", fill="x", pady=10)
self.pin = tk.Entry(self, bd = 4, relief="groove", show="*", font=20, justify='center')
self.pin.pack()
submit = tk.Button(self, text="Submit", width=12,font=tkfont.Font(size=12), command=lambda: [controller.show_frame("Options"), self.store_pin()], cursor="hand2")
submit.pack()
def store_pin(self):
print("test")
self.controller.pin_stored = self.pin.get()
class Options(StartPage):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
f1=tk.Frame(self)
f1.pack()
f2=tk.Frame(self)
f2.pack(side="bottom")
button1 = tk.Button(f1, text="Balance Inquiry",relief="flat", padx=30, justify='left', cursor="hand2", fg="red", font=tkfont.Font(size=10),
command=lambda: controller.show_frame("PageOne"))
button1.pack(side="left")
button2 = tk.Button(f1, text="Deposit",relief="flat", padx=50, justify='right', cursor="hand2", fg="red", font=tkfont.Font(size=10),
command=lambda: controller.show_frame("PageTwo"))
button2.pack(side="left")
button3 = tk.Button(f2, text="Withdraw",relief="flat", padx=50, justify='left', cursor="hand2", fg="red", font=tkfont.Font(size=10),
command=lambda: controller.show_frame("PageThree"))
button3.pack(side="left")
button4 = tk.Button(f2, text="Pin Change",relief="flat", padx=50, justify='right', cursor="hand2", fg="red", font=tkfont.Font(size=10),
command=lambda: controller.show_frame("PageFour"))
button4.pack(side="left")
tk.Button(self, text="print stored pin", command=self.print_pin).pack()
def print_pin(self):
print(self.controller.pin_stored)
if __name__ == "__main__":
SampleApp().mainloop()
Upvotes: 1