函数在我自己调用之前会自动调用



我正在尝试制作一个验证应用程序的GUI版本,该版本可以检查CSV电子表格,并检查用户输入的数据是否与电子表格上的任何数据匹配。

我遇到的问题是,在我在代码中调用它们之前,我使用的函数开始自动调用它们

她是运行良好的脚本的无GUI版本:

import csv
def logged ():
    print("You have succesfully logged in!")
def wrong ():
    print("You never got the correct log in details!")
    login()
def login (x, y):
    with open("login_details.csv") as file:
        read = csv.reader(file, delimiter=",")
        log = []
        pas = []
        for i in read:
            l = i[0]
            p = i[1]
            log.append(l)
            pas.append(p)
        try:
            logindx = log.index(x)
            pasindx = pas.index(y)
            if(logindx == pasindx):
                logged()
            else:
                wrong()
        except Exception as e:
            wrong()
login()

我现在遇到的问题是,我使用的是GUI,而wrong()函数似乎将其称为self。

我会发布完整的代码,使其易于理解

from tkinter import *
import csv
#import logon
def wrong():
    print("You got the Wrong login combination!")
    enter.login()
def logged ():
    print("You have successfuly loggoed in!")
class enter:
    def login(x, y):
        with open("login_details.csv") as file:
            read = csv.reader(file, delimiter=",")
            log = []
            pas = []
            for i in read:
                l = i[0]
                p = i[1]
                log.append(l)
                pas.append(p)
            try:
                logindx = log.index(x)
                pasindx = pas.index(y)
                if(logindx == pasindx):
                    logged()
                else:
                    wrong()
            except Exception as e:
                wrong()

#Window
root = Tk()
root.title("Login")
root.geometry("250x250")
root.configure(bg="white")

main = Frame(root, bg="white")
main.pack()
main.place(height=100, x=25, y=10)
#Username
usr_lbl = Label(main, text="Username: ", bg="white")
usr_lbl.grid(row=0, column=0)
usr_inp = Entry(main, bg="light grey")
usr_inp.grid(row=0, column=1)
#Password
pass_lbl = Label(main, text="Password: ", bg="white")
pass_lbl.grid(row=1, column=0)
pass_inp = Entry(main, bg="light grey")
pass_inp.grid(row=1, column=1)
#Enter
enter = Button(main, bg="light grey", text="Enter", command=enter.login(usr_inp.get(), pass_inp.get()))
enter.grid()
enter.place(width=100, y=45, x=50)

root.mainloop()

我得到的错误是:

"You got the Wrong login combination!"

(这是wrong()功能打印的内容)

  File ..., line 23, in login
    logindx = log.index(x)
ValueError: '' is not in list
During handling of the above exception, another exception occurred:
  File ..., line 55, in <module>
    enter = Button(main, bg="light grey", text="Enter", command=enter.login(usr_inp.get(), pass_inp.get()))
  File ..., line 30, in login
    wrong()
  File ..., line 7, in wrong
    enter.login()
TypeError: login() missing 2 required positional arguments: 'x' and 'y' 

这使得函数在我有机会调用它之前就好像在调用它。我该如何解决这个问题?

创建按钮时调用enter.login()方法:

enter = Button(main, bg="light grey", text="Enter", command=enter.login(usr_inp.get(), pass_inp.get()))

您将enter.login()结果传递给command参数,而不是让按钮在用户按下按钮时调用它。

添加lambda匿名函数:

enter = Button(main, bg="light grey", text="Enter",
   command=lambda: enter.login(usr_inp.get(), pass_inp.get()))

现在,一个函数对象(由lambda产生)被传递给命令参数,该参数在按下按钮时被调用。然后,lambda调用usr_inp.get()pass_inp.get(),并将结果传递给enter.login()

最新更新