I'm trying to program a small login window, but I seem to be stuck on my understanding of classes, functions and attributes. it keeps giving me an error saying MainWindow.changepage() missing 1 required positional argument: 'self'
.
My code is also a bit unorganized because I'm just moving stuff around hoping to understand and fix it.
import customtkinter as tk
class Login(tk.CTkFrame):
def __init__(self, parent):
super().__init__(parent)
global username
global password
username = tk.CTkEntry(self, justify="center", placeholder_text="username", width=200)
username.pack(pady=20)
password = tk.CTkEntry(self, justify="center", placeholder_text="password", width=200)
password.pack()
button = tk.CTkButton(self, text="Login", command=Check.check_user)
button.pack(pady=20)
self.pack(padx=10, pady=10)
class Check():
def check_user():
user = username.get()
passw = password.get()
if user in users and passw == users[user]:
print("welcome " + user)
main = MainWindow
main.changepage()
else:
print("incorrect username or password")
class SecondWindow(tk.CTkFrame):
def __init__(self, parent):
super().__init__(parent)
tk.CTkLabel(self, text="Welcome to jadajada").pack(pady=10,padx=10)
self.pack(padx = 10, pady=10)
class MainWindow():
def __init__(self, master):
mainframe = tk.CTkFrame(master)
mainframe.pack(padx= 10, pady= 10, fill= "both", expand = True)
self.index = 0
self.framelist = [Login(mainframe), SecondWindow(mainframe)]
self.framelist[1].forget()
def changepage(self):
self.framelist[self.index].forget()
self.index = 1
self.framelist[self.index].tkraise()
self.framelist[self.index].pack(pady=10,padx=10)
#details
users = dict(namviper="luapnam23", paul="grellmann", luap="nam")
#main window
root = tk.CTk()
root.geometry("500x500")
root.title("Login System")
tk.set_appearance_mode("dark")
tk.set_default_color_theme("green")
#main systems
window = MainWindow(root)
#mainloops
root.mainloop()
Your error is here:
class Check():
def check_user():
user = username.get()
passw = password.get()
if user in users and passw == users[user]:
print("welcome " + user)
main = MainWindow # error here
main.changepage()
else:
print("incorrect username or password")
I'm pretty sure you meant to instantiate (create an instance of a class). For this you need to add parentheses after MainWindow
and pass the proper parameters such that the line becomes MainWindow(root)
. You may need to rework your code so that you can pass the correct parameter. An alternative would be to pass the MainWindow object (main
) itself to Check.check_user.
Other note, your Check class is useless as it stores no data and only has one function. This could simply be a function.