I know this has been asked a bunch of times before, but i cannot work out how to apply the examples shown to my own code. I have only being developing 3 months, i am currently making a health app just for fun.
Below is the following code i think is relevant to the question.
These are the labels/fields for username and password entry (located in the SignUp class)
#Username label/entry
self.login_username_label = tk.Label(self.signinframe, text="Username:", fg='black', bg='light cyan', font=xsml_font)
self.login_username_label.grid(row=1, column=0, padx=(10,0), pady=(25,0))
self.login_username_entry = tk.Entry(self.signinframe, width=15, textvariable=self.USERNAME_ENTRY)
self.login_username_entry.grid(row=1, column=1, padx=(0,0), pady=(25,0))
#Password label/entry
self.login_password_label = tk.Label(self.signinframe, text="Password:", fg='black', bg='light cyan', font=xsml_font)
self.login_password_label.grid(row=2, column=0, padx=(10,0), pady=(15,0))
self.login_password_entry = tk.Entry(self.signinframe, show="*", width=15, textvariable=self.PASSWORD_ENTRY)
self.login_password_entry.grid(row=2, column=1, padx=(0,0), pady=(15,0))
#Sign in button - COMMAND = self.sign_in
self.sign_in_btn = tk.Button(self.signinframe, text="Sign In", command=self.sign_in, fg='white', \
bg='light sea green')
self.sign_in_btn.grid(row=3, column=1, padx=(0,20), pady=(15,0), sticky=tk.E)
self.sign_in_btn.config(font=('Calibri', 14))
This is the function to sign in which works fine (located in the SignUp class)
def sign_in(self):
#Connect to db / create cursor
db = sqlite3.connect('user.db')
c = db.cursor()
c.execute('SELECT * from users WHERE username="%s" AND password="%s"' % (self.login_username_entry.get(), self.login_password_entry.get(),))
#If record found show login success label, then launch def launch
if c.fetchone() is not None:
#login failed label
self.login_succ_lbl = tk.Label(self.signinframe, text="Login Success.", fg='green',\
bg='light cyan', font=sml_font)
self.login_succ_lbl.place(relx=0.45, rely=0.7)
#after 1500 launch home window
self.main.after(1500, self.launch)
#else show login failed label and clear fields for user to retry
else:
#clear username/password fields
self.login_username_entry.delete(0, tk.END)
self.login_password_entry.delete(0, tk.END)
#login failed label
self.login_fail_lbl = tk.Label(self.signinframe, text="Login failed. ", fg='red',\
bg='light cyan', font=sml_font)
self.login_fail_lbl.place(relx=0.45, rely=0.7)
All of this code shown is contained in this class
#SIGN UP CLASS (THIS IS THE SIGN UP/LOGIN PAGE)
class SignUp:
def __init__(self, main):
self.main = main
#to use across classes as needed
self.USERNAME_ENTRY = tk.StringVar()
self.PASSWORD_ENTRY = tk.StringVar()
#Make sign up window non-resizable
self.main.resizable(width=False, height=False)
#call logo class
logo = Logo(main)
#MAIN FRAME FOR SIGNUP (OUTER FRAME THAT CONTAINS ALL INNER FRAMES)
self.mainframe = tk.LabelFrame(self.main, text='Welcome to Health Tracker', fg='black', \
bg='light cyan', labelanchor=tk.NW, width=660, height=560, borderwidth=2, highlightthickness=0, font=lge_font)
self.mainframe.grid(row=1, column=0, padx=(5,5), pady=5, ipadx=15, ipady=15, sticky=tk.E)
#Force frame size for sign up
self.mainframe.grid_propagate(False)
When the user signs in it takes them to a new page, which is this class
class Home:
def __init__(self, main):
self.main = main
self.main.geometry("900x880")
self.main.title("Welcome to Health Tracker")
self.main.config(bg='white')
#non resizable window
self.main.resizable(width=False, height=False)
I want to show to user details after login, using the self.USERNAME_ENTRY and self.PASSWORD_ENTRY variables. But as they are part of the SignUp class, they are not recognised in the Home class. I have tried to call them by defining a tk.StringVar() for each in the SignUp class, and then calling them in the Home class, but this doesnt work. Here is what i have attempted.
#USER STATS (part of the Home class)
conn = sqlite3.connect("user.db")
c = conn.cursor()
c.execute('SELECT * from users WHERE username="%s" AND password="%s"' % (SignUp.USERNAME_ENTRY.get(), SignUp.PASSWORD_ENTRY.get(),))
record = c.fetchone()
How can i pass the values of self.USERNAME_ENTRY and self.PASSWORD_ENTRY from the SignUp class and into the Home class ?
Also if relevant, the SignUp class is called using the if name == main command as so
#RUN TKINTER MAINLOOP
def main():
root = tk.Tk()
#Declare the SignUp class to init program
app = SignUp(root)
#root config
root.geometry('700x690')
root.title("Sign Up To Health Tracker")
root.config(bg='white')
#LOGOFRAME for app logo
#Define logoframe size/position
logoframe = tk.LabelFrame(root, bg="white", width=480, height=90, borderwidth=0, highlightthickness=0)
logoframe.grid(row=0, column=0, columnspan=4, ipadx=25, sticky=tk.N)
#force width and height on frame
logoframe.grid_propagate(0)
#Define logo image and grid position
logo = tk.PhotoImage(file = "logo.png")
logo_label = tk.Label(logoframe, image=logo, borderwidth=0, highlightthickness=0)
logo_label.grid(row=0, column=0)
#run mainloop for program
tk.mainloop()
#run main if being run as standalone
if __name__ == '__main__':
main()