简体   繁体   中英

TypeError: mainloop() missing 1 required positional argument: 'self'

I am trying to code a little login/sign-in GUI in tkinter. When the program starts, the Login window pops up. When I press the "Sign in" button, I get this: TypeError: mainloop() missing 1 required positional argument: 'self'

Here's the code I'm working with:

#importing
import tkinter as tk

#creating all of the widgets for the login
root = tk.Tk()
root.geometry("200x120")
loglab = tk.Label(text="Login")
userlab = tk.Label(text="Username")
passlab = tk.Label(text="Password")
username = tk.Entry(root)
password = tk.Entry(root,show="*")


#defining the button functions
def signin():
    root.destroy
    signroot = tk.Toplevel
    userlab = tk.Label(text="Username")
    passlab = tk.Label(text="Password")
    username = tk.Entry(root)
    password = tk.Entry(root,show="*")
    signroot.mainloop()

#creating the login root buttons
signb = tk.Button(text="Sign In",command=signin)

#gridding the widgets
loglab.grid(row=0,column=0,pady=10)
userlab.grid(row=1,column=0)
username.grid(row=1,column=1,padx = 5)
passlab.grid(row=2,column=0)
password.grid(row=2,column=1,padx = 5)
signb.grid(row=3,column=0,pady=5)

root.mainloop()

Try this:

#importing
import tkinter as tk

wrong_password_label = None

#defining the button functions
def signin():
    global wrong_password_label
    # Check if the password the user entered is: "my password"
    if password.get() == "my password":
        root.destroy()
        signroot = tk.Tk()
        userlab = tk.Label(signroot, text="You are logged in!")
        userlab.grid(row=1, column=1)
        signroot.mainloop()
    else:
        # If the password is wrong:
        if wrong_password_label is None:
            # If there isn't a `wrong_password_label` widget defined:
            wrong_password_label = tk.Label(root, text="Wrong Password", fg="red")
            wrong_password_label.grid(row=4, column=0, columnspan=2)
        else:
            # If there is a `wrong_password_label` widget defined:
            wrong_password_label.config(text="Still wrong")


#creating all of the widgets for the login
root = tk.Tk()
# root.geometry("200x120") # You don't need this when using `.grid` or `.pack`
loglab = tk.Label(root, text="Login")
userlab = tk.Label(root, text="Username")
passlab = tk.Label(root, text="Password")
username = tk.Entry(root)
password = tk.Entry(root, show="*")

#creating the login root buttons
signb = tk.Button(root, text="Sign In", command=signin)

#gridding the widgets
loglab.grid(row=0, column=0, pady=10)
userlab.grid(row=1, column=0)
username.grid(row=1, column=1, padx = 5)
passlab.grid(row=2, column=0)
password.grid(row=2, column=1, padx = 5)
signb.grid(row=3, column=0, pady=5)

root.mainloop()

I think that is what you were trying to do. It checks if the password is my password and if it is, it destroys the old window and creates a new one. If the password is wrong, it tells the user. You can also add a check for the username.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM