功能在我自己调用之前自动调用?

时间:2015-02-18 17:04:22

标签: python function class csv python-3.x

我正在尝试制作一个验证应用程序的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()函数似乎自称为。

我会发布完整的代码,因此很容易理解

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' 

这使得在我有机会调用它之​​前,函数似乎正在调用。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您在创建按钮时调用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生成)传递给command参数,该参数在按下按钮时被调用。然后lambda调用usr_inp.get()和{ {1}}并将结果传递给pass_inp.get()