tkinter button function inside of page class - python

import tkinter as tk
class program(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args,**kwargs)
self.title("Staff Management System")
self.geometry("1280x720")
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
self.frames = {}
#frame that will hold all the elements
container = tk.Frame(self)
container.grid(row=0,column=0, sticky="nsew")
container.columnconfigure(0,weight=1)
container.rowconfigure(0,weight=1)
container.configure(background="blue")
#listing frames (pages) to be controlled
for F in (homePage, staffLogin):
frame = F(parent=container, controller=self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(homePage)
def show_frame(self, page):
frame = self.frames[page]
frame.tkraise()
class homePage(tk.Frame):
def __init__(self, parent, controller, *args, **kwargs):
tk.Frame.__init__(self,parent,*args, **kwargs)
for i in range(5):
self.columnconfigure(i, weight=1)
for i in range(5):
self.rowconfigure(i, weight=1)
promotionsButton = tk.Button(self, text="Promotions", height = 4)
promotionsButton.grid(row=5, column =0, sticky='w')
staffLoginButton = tk.Button(self, text="Staff Login", width = 50, height = 20, command= lambda: controller.show_frame(staffLogin))
staffLoginButton.grid(row=2, column=1)
managerLoginButton = tk.Button(self, text="Manager Login", width = 50, height = 20)
managerLoginButton.grid(row=2,column=2)
class staffLogin(tk.Frame):
def __init__(self, parent, controller, *args, **kwargs):
tk.Frame.__init__(self,parent, *args, **kwargs)
self.controller = controller
for i in range(128):
self.columnconfigure(i,weight=1)
for i in range(128):
self.rowconfigure(i,weight=1)
self.staffLabel = tk.Label(self, text = "Staff Login", font=("Helvectia", 24))
self.staffLabel.grid(row = 40, column=60)
self.userIDLabel = tk.Label(self, text = "UserID:")
self.userIDLabel.grid(sticky="W", row=67,column=59)
self.userIDInput = tk.Entry(self, width=50, font=("Helvectia",16))
self.userIDInput.grid(row=67, column=60)
self.userPasswordLabel = tk.Label(self, text = "Password:")
self.userPasswordLabel.grid(sticky="W", row=70, column=59)
self.userPasswordInput = tk.Entry(self,width=50, font=("Helvectia",16))
self.userPasswordInput.grid(row=70, column=60)
self.showPassword = tk.Button(self, text="Show Password", command=self.togglePassword())
self.showPassword.grid(sticky="W", row=71, column=60)
self.loginButton = tk.Button(self, text="Login", height=2, width=7)
self.loginButton.grid(sticky="E",row = 71, column=60)
self.backButton = tk.Button(self, text="Back To Main Menu", height=4, command= lambda: controller.show_frame(homePage))
self.backButton.grid(row=128, column=0)
def togglePassword(self):
if self.userPasswordLabel.cget("show") == "*":
self.userPasswordLabel.config(show= "")
self.showPassword.config(text="Hide Password")
else:
self.userPasswordLabel.config(show="")
self.showPassword.config(text="Show Password")
thing = program()
thing.mainloop()
im trying to create a function that runs when clicked inside of my "staff login" page. However, due to me using a new method of programming tkinter to be able to switch pages, I am unsure of how to get functions for buttons to work. currently, i get an error saying that the methods that i am trying to call do not exist. im not sure of what to do in order to be able to run functions inside of pages

Related

How do I make my login button follow through : tkinter

I've made this tkinkter home screen page with 3 buttons. However, when I click the buttons it comes up with error and doesn't move on to the login screen. Can anyone help?
My code:
import tkinter as tk
class program(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args,**kwargs)
self.title("Cars4You")
self.geometry("750x1450")
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
self.frames = {}
container = tk.Frame(self)
container.grid(row=0,column=0, sticky="nsew")
container.columnconfigure(0,weight=1)
container.rowconfigure(0,weight=1)
container.configure(background="blue")
for F in (homePage, staffLogin):
frame = F(parent=container, controller=self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(homePage)
def show_frame(self, page):
frame = self.frames[page]
frame.tkraise()
class homePage(tk.Frame):
def __init__(self, parent, controller, *args, **kwargs):
tk.Frame.__init__(self,parent,*args, **kwargs)
for i in range(5):
self.columnconfigure(i, weight=1)
for i in range(5):
self.rowconfigure(i, weight=1)
staffLoginButton = tk.Button(self, text="Staff Login", width = 50, height = 20, command= lambda: controller.show_frame(staffLogin))
staffLoginButton.grid(row=2, column=1)
managerLoginButton = tk.Button(self, text="Owner Login", width = 50, height = 20)
managerLoginButton.grid(row=2,column=2)
customerLoginButton = tk.Button(self, text="Customer Login", width = 50, height = 20)
customerLoginButton.grid(row=2,column=3)
class staffLogin(tk.Frame):
def __init__(self, parent, controller, *args, **kwargs):
tk.Frame.__init__(self,parent, *args, **kwargs)
self.controller = controller
for i in range(128):
self.columnconfigure(i,weight=1)
for i in range(128):
self.rowconfigure(i,weight=1)
self.staffLabel = tk.Label(self, text = "Staff Login", font=("Helvectia", 24))
self.staffLabel.grid(row = 40, column=60)
self.userIDLabel = tk.Label(self, text = "UserID:")
self.userIDLabel.grid(sticky="W", row=67,column=59)
self.userIDInput = tk.Entry(self, width=50, font=("Helvectia",16))
self.userIDInput.grid(row=67, column=60)
self.userPasswordLabel = tk.Label(self, text = "Password:")
self.userPasswordLabel.grid(sticky="W", row=70, column=59)
self.userPasswordInput = tk.Entry(self,width=50, font=("Helvectia",16))
self.userPasswordInput.grid(row=70, column=60)
self.showPassword = tk.Button(self, text="Show Password", command=self.togglePassword())
self.showPassword.grid(sticky="W", row=71, column=60)
self.loginButton = tk.Button(self, text="Login", height=2, width=7)
self.loginButton.grid(sticky="E",row = 71, column=60)
self.backButton = tk.Button(self, text="Back To Main Menu", height=4, command= lambda: controller.show_frame(homePage))
self.backButton.grid(row=128, column=0)
def togglePassword(self):
if self.userPasswordLabel.cget("show") == "*":
self.userPasswordLabel.config(show= "")
self.showPassword.config(text="Hide Password")
else:
self.userPasswordLabel.config(show="")
self.showPassword.config(text="Show Password")
thing = program()
thing.mainloop()
Removed in line 80 command=self.togglePassword()
replaced "show" to "text" in line 90 if self.userPasswordLabel.cget("text") == "*":
Code:
import tkinter as tk
class program(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args,**kwargs)
self.title("Cars4You")
self.geometry("750x1450")
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
self.frames = {}
container = tk.Frame(self)
container.grid(row=0,column=0, sticky="nsew")
container.columnconfigure(0,weight=1)
container.rowconfigure(0,weight=1)
container.configure(background="blue")
for F in (homePage, staffLogin):
frame = F(parent=container, controller=self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(homePage)
def show_frame(self, page):
frame = self.frames[page]
frame.tkraise()
class homePage(tk.Frame):
def __init__(self, parent, controller, *args, **kwargs):
tk.Frame.__init__(self,parent,*args, **kwargs)
for i in range(5):
self.columnconfigure(i, weight=1)
for i in range(5):
self.rowconfigure(i, weight=1)
staffLoginButton = tk.Button(self, text="Staff Login", width = 50, height = 20, command= lambda: controller.show_frame(staffLogin))
staffLoginButton.grid(row=2, column=1)
managerLoginButton = tk.Button(self, text="Owner Login", width = 50, height = 20)
managerLoginButton.grid(row=2,column=2)
customerLoginButton = tk.Button(self, text="Customer Login", width = 50, height = 20)
customerLoginButton.grid(row=2,column=3)
class staffLogin(tk.Frame):
def __init__(self, parent, controller, *args, **kwargs):
tk.Frame.__init__(self,parent, *args, **kwargs)
self.controller = controller
for i in range(128):
self.columnconfigure(i,weight=1)
for i in range(128):
self.rowconfigure(i,weight=1)
self.staffLabel = tk.Label(self, text = "Staff Login", font=("Helvectia", 24))
self.staffLabel.grid(row = 40, column=60)
self.userIDLabel = tk.Label(self, text = "UserID:")
self.userIDLabel.grid(sticky="W", row=67,column=59)
self.userIDInput = tk.Entry(self, width=50, font=("Helvectia",16))
self.userIDInput.grid(row=67, column=60)
self.userPasswordLabel = tk.Label(self, text = "Password:")
self.userPasswordLabel.grid(sticky="W", row=70, column=59)
self.userPasswordInput = tk.Entry(self,width=50, font=("Helvectia",16))
self.userPasswordInput.grid(row=70, column=60)
self.showPassword = tk.Button(self, text="Show Password")
self.showPassword.grid(sticky="W", row=71, column=60)
self.loginButton = tk.Button(self, text="Login", height=2, width=7)
self.loginButton.grid(sticky="E",row = 71, column=60)
self.backButton = tk.Button(self, text="Back To Main Menu", height=4, command= lambda: controller.show_frame(homePage))
self.backButton.grid(row=128, column=0)
def togglePassword(self):
if self.userPasswordLabel.cget("text") == "*":
self.userPasswordLabel.config(show= "")
self.showPassword.config(text="Hide Password")
else:
self.userPasswordLabel.config(show="")
self.showPassword.config(text="Show Password")
thing = program()
thing.mainloop()
Output:
Output Staff Login:
There are few issues in your code:
show="*" should be set when creating self.userPasswordInput
command=self.togglePassword() should be command=self.togglePassword instead
self.userPasswordLabel should be self.userPasswordInput instead inside togglePassword()
inside togglePassword(), show="" is used in both if and else block. show="*" should be used in else block
Below is the required changes:
class staffLogin(tk.Frame):
def __init__(self, parent, controller, *args, **kwargs):
...
self.userPasswordInput = tk.Entry(self,width=50, font=("Helvectia",16), show="*") # set show="*" initially
...
self.showPassword = tk.Button(self, text="Show Password", command=self.togglePassword) ### self.togglePassword() -> self.togglePassword
...
def togglePassword(self):
if self.userPasswordInput.cget("show") == "*":
self.userPasswordInput.config(show= "")
self.showPassword.config(text="Hide Password")
else:
self.userPasswordInput.config(show="*")
self.showPassword.config(text="Show Password")

tkinter frame not filling out parent container

import tkinter as tk
class program(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args,**kwargs)
self.title("Staff Management System")
self.geometry("1280x720")
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.frames = {}
#frame that will hold all the elements
container = tk.Frame(self)
container.grid(row=0,column=0)
container.columnconfigure(0,weight=1)
container.rowconfigure(0,weight=1)
#listing frames (pages) to be controlled
for F in (homePage, staffLogin):
frame = F(parent=container, controller=self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky = "nsew")
self.show_frame(homePage)
def show_frame(self, page):
frame = self.frames[page]
frame.tkraise()
class homePage(tk.Frame):
def __init__(self, parent, controller, *args, **kwargs):
tk.Frame.__init__(self,parent,*args, **kwargs)
for i in range(4):
self.columnconfigure(i, weight=1)
for i in range(4):
self.rowconfigure(i, weight=1)
self.config(background="red")
self.configure(background="red")
promotionsButton = tk.Button(self, text="Promotions", height = 4)
promotionsButton.grid(row=2, column = 0, sticky='w')
staffLoginButton = tk.Button(self, text="Staff Login", width = 50, height = 20, command= lambda: controller.show_frame(staffLogin))
staffLoginButton.grid(row=1, column=1)
managerLoginButton = tk.Button(self, text="Manager Login", width = 50, height = 20)
managerLoginButton.grid(row=1,column=2)
class staffLogin(tk.Frame):
def __init__(self, parent, controller, *args, **kwargs):
tk.Frame.__init__(self,parent, *args, **kwargs)
self.controller = controller
for i in range(128):
self.columnconfigure(i,weight=1)
for i in range(128):
self.rowconfigure(i,weight=1)
userIDLabel = tk.Label(self, text = "UserID:")
userIDInput = tk.Entry(self, width=50, font=("Helvectia",16))
userIDLabel.grid(sticky="W", row=67,column=59)
userIDInput.grid(row=67, column=60)
userPasswordLabel = tk.Label(self, text = "Password:")
userPasswordInput = tk.Entry(self,width=50, font=("Helvectia",16))
userPasswordLabel.grid(sticky="W", row=70, column=59)
userPasswordInput.grid(row=70, column=60)
loginButton = tk.Button(self, text="Login", height=2, width=7)
loginButton.grid(sticky="E",row = 71, column=60)
thing = program()
thing.mainloop()
for each "page" i want the frame to fill out the entire window but when i run my program, my page does not fill out the whole window. i've tried setting the weight of both the root (self) and the containers that are holding my frames but to no avail. i am unsure as of what to do next. in each page i ran the "self.configure" method in order to check whether the frame has filled out the entire window but it has not
The frames are filling your container, but your container isn't filling the window. The container is a child of self, and you use grid to add the container, but you never set the weight of the row and column for self, so it defaults to zero. That means that the container won't be allocated any extra space which results in it being centered in the window. You also don't set the sticky attribute, so even if it was allocated the space it wouldn't use it.
Personally, I recommend using pack for container since it requires fewer lines of code. However, to use grid you need to do it like this:
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
container.grid(row=0,column=0, sticky="nsew")

Value submitted through tkinter login page is not reflected in the database

I am designing an app in python 3 and I am using xampp as database.
I have a LogIn Page and Home Page. The idea is that when I login by entering the credentials there is navigation between the pages and the credentials are reflected in the database. Here the pages are treated as frames. I am able to navigate between the frames.
My code shows no error but the entry made in the log in page is not reflected in the database.
The code is as shown below
import tkinter as tk
from tkinter import ttk
import mysql.connector
from tkinter import messagebox
class App(tk.Tk):
bg_img_path = "images\\bg9.png"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.geometry("1500x750")
main_frame = tk.Frame(self, width=200, height=50, highlightbackground="black", highlightthickness=1,
background="#e6ffe6")
main_frame.pack(side='top', fill='both', expand='True')
main_frame.grid_rowconfigure(0, weight=1)
main_frame.grid_columnconfigure(0, weight=1)
self.bkgr_image = tk.PhotoImage(file=self.bg_img_path)
self.frames = {}
for F in (LoginPage,HomePage):
page_name = F.__name__
frame = F(main_frame, self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky='nsew')
self.show_frame("LoginPage")
def show_frame(self, container):
frame = self.frames[container]
frame.tkraise()
class BasePage(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.controller = controller
label_bkgr = tk.Label(self, image=controller.bkgr_image)
label_bkgr.place(x=0, y=0)
class LoginPage(BasePage):
def __init__(self, parent, controller):
super().__init__(parent, controller)
login_frame = tk.Frame(self, width=200, height=50, highlightbackground="black", highlightthickness=1,
background="#e6ffe6")
login_frame.grid(row=400, column=20, padx=500, pady=250)
self.label_title = tk.Label(login_frame, text="Log In", font=("Helvetica", 40), bg="#e6ffe6")
self.label_title.grid(row=0, column=20, padx=10, pady=10)
self.label_username = tk.Label(login_frame, text="Username", font=("Helvetica", 20), bg="#e6ffe6")
self.label_username.grid(row=50, column=20, padx=10, pady=10)
self.entry_username = tk.Entry(login_frame, width=15, font=("Helvetica", 20))
self.entry_username.grid(row=50, column=30, padx=10, pady=10)
self.label_password = tk.Label(login_frame, text="Password", font=("Helvetica", 20), bg="#e6ffe6")
self.label_password.grid(row=60, column=20, padx=10, pady=10)
self.entry_password = tk.Entry(login_frame, width=15, font=("Helvetica", 20))
self.entry_password.grid(row=60, column=30, padx=10, pady=10)
self.login_button = tk.Button(login_frame, text="Log In", command=lambda: [self.submit,self.controller.show_frame("HomePage")], font=("Helvetica", 20),
bg="#e6ffe6")
self.login_button.grid(row=70, column=25, padx=10, pady=10)
def submit(self):
self.u_name = self.entry_username.get()
self.p_word = self.entry_password.get()
employee = mysql.connector.connect(host="localhost", user="root", password="", database="edatabase")
cursor_variable = employee.cursor()
cursor_variable.execute("INSERT INTO login VALUES ('" + self.u_name + "','" + self.p_word + "')")
employee.commit()
employee.close()
messagebox.showinfo("Log In", "Succesfull")
class HomePage(BasePage):
def __init__(self, parent, controller):
super().__init__(parent, controller)
#self.menubar = tk.Menu(self)
#self.menubar.add_command(label = "Home")
#self.menubar.add_command(label = "Careers")
label1 = ttk.Label(self, text='Home', font=("Helvetica", 20))
label1.pack(padx=10, pady=10)
app = App()
app.mainloop()
Any help is appreciated.
the error is resolved. I forgot to put (). Then it becomes
self.login_button = tk.Button(login_frame, text="Log In", command=lambda: [self.submit(),self.controller.show_frame("HomePage")], font=("Helvetica", 20),
bg="#e6ffe6")

python tkinter creating a widget outside his frame

I want to create a widget outside his frame, but I don't know what's his master.
this is the structure.
first I created the class of the root. and then 3 classes of frames.
inside the class of the root I put a function. inside the function I created a text widget that should be located in the first one of the 3 frames
I really don't get what I should write as the master of my text widget to locate it in the first frame.
since I am a beginner if you have any advice I'd really appreciate.
thanks for attention here's the code
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
import datetime
LARGE_FONT = ("VERDANA", 12)
#user's information manager(classes and method)
class person:
def __init__(self, name, birthday, sex):
self.name = name
self.birthday = birthday
self.sex = sex
def age(self, name, birthday):
user = person(name, birthday, "male")
today = datetime.date.today()
print (today.year - self.birthday.year)
#main windows
class deathCalculatorapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "age calculator app")
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")
# all methods here
def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
def calculate(self, name, birthday):
user = person(name, birthday, "male")
text_answer = tk.Text(master = , height=3, width=30)
text_answer.grid(column=1, row=9)
answear_text = ("{name} is {age} years old".format(name=name_entry.get(), age=calculate()))
text_answer.insert(tk.END, answear_text)
print (user.age()
#all of the frames
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
#Labels
label = ttk.Label(self, text="deathcalculatorapp", font=LARGE_FONT)
label.grid(column=1, row=0)
first_label = ttk.Label(self, text = "insert your data")
name_label= tk.Label(self, text = "name", bg="lightblue")
year_label = tk.Label(self, text="year", bg ="lightblue", padx=9)
month_label = tk.Label(self, text= "month", bg = "lightblue", padx=3)
day_label = tk.Label(self, text ="day", bg= "lightblue", padx=11)
first_label.grid(column=1, row=3)
name_label.grid(column=0, row=4)
year_label.grid(column=0, row =5)
month_label.grid(column=0, row =6)
day_label.grid(column=0, row = 7)
#Entries
name_entry = tk.Entry(self, text = "", bg = "lightblue")
year_entry = tk.Entry(self,text = "", bg = "lightblue")
month_entry = tk.Entry(self, text = "", bg= "lightblue")
day_entry = tk.Entry(self, text= "", bg = "lightblue")
name_entry.grid(column=1, row=4)
year_entry.grid(column=1,row=5)
month_entry.grid(column=1, row= 6)
day_entry.grid(column=1, row=7)
#Radiobutton about sex
sexdatum = tk.IntVar()
female= ttk.Radiobutton(self, text="female",variable= sexdatum, value="female")
male=ttk.Radiobutton(self, text="male", variable= sexdatum, value="male")
female.grid(column=2, row=4)
male.grid(column=2, row=5)
#Buttons
calculate_button = ttk.Button(self, text="calculate your lifespawn",
command=lambda: controller.age(name_entry.get(),datetime.date(int(year_entry.get()),int(month_entry.get()),int(day_entry.get()))))
calculate_button.grid(column=1, row=8)
button1 = ttk.Button(self, text="Go to Page One",
command=lambda: controller.show_frame("PageOne"))
button2 = ttk.Button(self, text="Go to Page Two",
command=lambda: controller.show_frame("PageTwo"))
button1.grid(column=0, row=0)
button2.grid(column=0, row=1)
#text
#image
image = Image.open(r"/"
r"Users/tommasomasaracchio/Documents/pythonfolder/kushina4.jpg")
image.thumbnail((500,300), Image.ANTIALIAS)
photo = ImageTk.PhotoImage(image)
Photo_label= ttk.Label(self, image=photo)
Photo_label.image = photo
Photo_label.grid(row= 2, column = 1)
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = ttk.Label(self, text="This is page 1", font=LARGE_FONT)
label.grid(column=0, row=0)
button = ttk.Button(self, text="Go to the start page",
command=lambda: controller.show_frame("StartPage"))
button.grid(column=0, row=0)
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = ttk.Label(self, text="This is page 2", font=LARGE_FONT)
label.grid(column=0, row=0)
button = ttk.Button(self, text="Go to the start page",
command=lambda: controller.show_frame("StartPage"))
button.grid(column=0, row=0)
if __name__ == "__main__":
app = deathCalculatorapp()
app.mainloop()
It should be master = self.frames['StartPage'].

Tkinter change a shared variable

I am using a solution found to try and share variables throughout my code, which consists of 'Frame' classes. However, any attempt I make to try and change the value of these shared variables seems to have no effect, and after I attempt to change them, if I print it just returns blank. Any help would be appreciated.
class GolfApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.shared_data = {
"currentcourse": tk.StringVar(),
"numberofteams": tk.IntVar()}
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 (MainMenu, CreatePage, ViewPage, GetTeamsPage, ChooseCourse,
AddCourse, LoginSignUp, Login, SignUp, Highscores1, Highscores2,
Scorecards1, Scorecards2):
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("LoginSignUp")
def show_frame(self, page_name):
frame = self.frames[page_name]
frame.tkraise()
This is where the solution from the link can be found. I have two variables, 'currentcourse' and 'numberofteams' which I need to share from one frame to others. I am attempting to set these variables in two different classes in the following bits of code.
class GetTeamsPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.configure(background="lightgreen")
def set_teamnumber():
numberofteams = answerentry.get()
numberofteams = self.controller.shared_data["numberofteams"].get()
def testInt(inStr, i, acttyp):
ind = int(i)
if acttyp == '1':
if not inStr[ind].isdigit():
return False
return True
for col in range(7):
self.grid_columnconfigure(col)
for row in range(5):
self.grid_rowconfigure(row)
questionlbl = tk.Label(self,
text="How many teams/players are there?",
bg="lightgreen",
font = "Verdana 20 bold")
questionlbl.grid(column=2,
row=0,
columnspan=3)
answerentry = tk.Entry(self,
text="Enter a number here.",
validate = "key",
textvariable=self.controller.shared_data["numberofteams"])
answerentry.grid(column=2,
row=2,
columnspan=3)
moveonbtn = tk.Button(self,
text="Continue",
height = "3",
width = "40",
bg="darkgreen",
fg="lightgreen",
command = lambda: (controller.show_frame("CreatePage"), set_teamnumber()))
moveonbtn.grid(column=1,
row=5,
columnspan=3)
returnbtn = tk.Button(self,
height="3",
width="40",
bg="darkgreen",
fg="lightgreen",
text="Return to main menu",
command = lambda: controller.show_frame("MainMenu"))
returnbtn.grid(column=4,
row=5,
columnspan=3)
class ChooseCourse(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.configure(background="lightgreen")
cursor.execute("SELECT CourseName FROM Course")
coursetuple = cursor.fetchall()
courselist = [row[0] for row in coursetuple]
def get_choice():
currentcourse = self.controller.shared_data["currentcourse"]
currentcourse = listmenu.get()
for col in range(2):
self.grid_columnconfigure(col, minsize=50)
for row in range(7):
self.grid_rowconfigure(row, minsize=60)
titlelbl = tk.Label(self,
text="Choose a course",
bg="lightgreen",
font = "Verdana 20 bold")
titlelbl.grid(column=2,
row=0)
addbtn = tk.Button(self,
text="Add a new course",
bg="darkgreen",
fg="lightgreen",
command = lambda: controller.show_frame("AddCourse"))
addbtn.grid(column=2,
row=3)
continuebtn = tk.Button(self,
text="Continue",
bg="darkgreen",
fg="lightgreen",
command = lambda: (controller.show_frame("GetTeamsPage"), get_choice))
continuebtn.grid(column=2,
row=4)
returnbtn = tk.Button(self,
text="Return to main menu",
bg="darkgreen",
fg="lightgreen",
command = lambda: controller.show_frame("MainMenu"))
returnbtn.grid(column=2,
row=5)
listmenu = tk.Listbox(self)
for x in range(0, len(courselist)):
listmenu.insert("end", courselist[x])
listmenu.grid(column=2,
row=1)
You start by setting shared_data["current_course"] to an instance of StringVar, but then later you're resetting it to just a string.
Since it is a StringVar, you need to call the set method to set the value:
currentcourse = self.controller.shared_data["currentcourse"]
currentcourse.set(listmenu.get())

Categories

Resources