簡體   English   中英

類型錯誤:mainloop() 缺少 1 個必需的位置參數:'self'

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

我正在嘗試在 tkinter 中編寫一些登錄/登錄 GUI。 程序啟動時,會彈出Login window。 當我按下“登錄”按鈕時,我得到這個: TypeError: mainloop() missing 1 required positional argument: 'self'

這是我正在使用的代碼:

#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()

嘗試這個:

#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()

我認為這就是你想要做的。 它檢查密碼是否是my password ,如果是,它會破壞舊的 window 並創建一個新密碼。 如果密碼錯誤,它會告訴用戶。 您還可以為用戶名添加檢查。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM