pythonpython-3.xuser-interfacetkinterlogin-page

Error: login() missing 1 required positional argument


I tried coding a login page using tkinter, but I'm getting an error. Here is my code:

from tkinter import *

def register(event):
    screen1 = Toplevel(screen)
    screen1.title("Register")
    screen1.geometry("320x250")
    username =StringVar()
    password =StringVar()
    contact  =StringVar()
    email    =StringVar()
    Label(screen1,text="Please enter your details below").pack()
    Label(screen1,text="Username").pack()
    Entry(screen1,textvariable = username)
    Label(screen1,text="Contact").pack()
    Entry(screen1, textvariable=contact)
    Label(screen1, text="Email id").pack()
    Entry(screen1, textvariable=email)
    Label(screen1, text="Password").pack()
    Entry(screen1, textvariable=password)
    Button(screen1, text="Register", width=10, height=1).pack()

def login(event):
    screen2 = Toplevel(screen)
    screen2.title("Register")
    screen2.geometry("320x250")
    username = StringVar()
    password = StringVar()
    Label(screen2, text="Username").pack()
    Entry(screen2, textvariable=username)
    Label(screen2, text="Password").pack()
    Entry(screen2, textvariable=password)
    Button(screen2, text="Login", width=10, height=1).pack()

def main_screen():
    global screen
    screen = Tk()     #creating tk window
    screen.geometry("320x250")
    screen.title= ("notes 1.0")
    Label(screen,text="").pack()
    Button(screen,text="Login",height="2", width="30",command=login).pack()
    Label(screen,text="").pack()
    Button(screen,text="Register",height="2", width="30",command=register).pack()
    screen.mainloop()
main_screen()

I get this error:

Traceback (most recent call last):
  File "C:\Users\Matrena\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
TypeError: register() missing 1 required positional argument: 'event'
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Matrena\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
TypeError: login() missing 1 required positional argument: 'event'

Solution

  • Here. First off, you shouldn't pass event to the register() and login() functions. If you use the .bind() function, then you have to use event, but otherwise, that will give you an error.

    Here is your code:

    from tkinter import *
    screen = Tk()     #creating tk window
    screen.geometry("320x250")
    screen.title("notes 1.0")
    Label(screen,text="").pack()
    def login():
        screen2 = Toplevel()
        screen2.title("Register")
        screen2.geometry("320x250")
        username = StringVar()
        password = StringVar()
        Label(screen2, text="Username").pack()
        Entry(screen2, textvariable=username)
        Label(screen2, text="Password").pack()
        Entry(screen2, textvariable=password)
        Button(screen2, text="Login", width=10, height=1).pack()
    def register():
        screen1 = Toplevel(screen)
        screen1.title("Register")
        screen1.geometry("320x250")
        username =StringVar()
        password =StringVar()
        contact  =StringVar()
        email    =StringVar()
        Label(screen1,text="Please enter your details below").pack()
        Label(screen1,text="Username").pack()
        Entry(screen1,textvariable = username)
        Label(screen1,text="Contact").pack()
        Entry(screen1, textvariable=contact)
        Label(screen1, text="Email id").pack()
        Entry(screen1, textvariable=email)
        Label(screen1, text="Password").pack()
        Entry(screen1, textvariable=password)
        Button(screen1, text="Register", width=10, height=1).pack()
    Button(screen,text="Login",height="2", width="30",command = login).pack()
    Label(screen,text="").pack()
    Button(screen,text="Register",height="2", width="30",command=register).pack()
    
    
    
    
    screen.mainloop()
    

    I have noticed the project isn't finished, though, so if you want me to finish it, just ping me!!

    Hope this helps!