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

function automatically calls before i call it myself?

我正在尝试制作一个验证应用程序的 GUI 版本来检查 CSV 传播 sheet 并检查用户输入的数据是否与传播中的任何数据相匹配sheet.

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

她是运行良好的脚本的 None-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() 函数似乎调用它自己。

我会 post 完整的代码以便于理解

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()result 传递给 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().