TypeError:leftClick()缺少1个必需的位置参数:'password'

时间:2015-05-10 05:46:30

标签: python user-interface python-3.x tkinter

我是Python的新手,正在尝试创建一个登录窗口。当用户输入用户名和密码时,程序将检索保存到文本文件中的设置用户名和密码,如果匹配,则用户将能够使用该程序。但是,我在尝试运行程序时不断收到此消息。这是我的代码:

    from tkinter import *
    login = Tk()
    Label(login, text ="Username").grid(row = 0)
    Label(login, text ="Password").grid(row = 1)

    username = StringVar()
    password = StringVar()

    i1 = Entry(login, textvariable=username)
    i2 = Entry(login, textvariable=password, show = "*")

    i1.grid(row = 0, column = 1)
    i2.grid(row = 1, column = 1)

    def leftClick(username, password):
            f = open('godhelpme.txt', 'r')
            file = open('some_file.txt', 'r')
            if username == (f.read()):
                    if password == (file.read()):
                            print ("HELLOOOO")
                    else:
                            print ("invalid username/password")
            else: print ("invalid username/password")


    button2 = Button(login, text = "Log In", command = save)
    button2.grid(columnspan = 2)
    button2.bind("<Button-1>", leftClick)



    login.mainloop()

这就是我尝试为我的按钮添加两个函数的方法

    from tkinter import *
    from tkinter import ttk


    def win1():
        mainframe = ttk.Frame(root, padding = '3 3 12 12')
        mainframe.grid(column = 0, row = 0, sticky = (N, W, E, S))
        mainframe.columnconfigure(0, weight = 1)
        mainframe.rowconfigure(0, weight = 1)

        ttk.Button(mainframe, text = 'Register', command = register).grid(
            column = 1, row = 1, sticky = W)
        ttk.Button(mainframe, text = 'Log In', command = login).grid(
            column = 1, row = 2, sticky = W)

        root.mainloop()

    def combine_funcs(save, quit):
        def combined_func(save, quit):
            for f in funcs:
                f(save, quit)
        return combined_funcs

    def quit():
        root.withdraw()

    def register():
        quit()
        def save():
            import json as serializer
            with open('godhelpme.txt', 'w') as f:
                serializer.dump(username.get(), f)
            with open('some_file.txt', 'w') as f:
                serializer.dump(password.get(), f)
            register.quit()

        register = Tk()
        Label(register, text ="Username").grid(row = 0)
        Label(register, text ="Password").grid(row = 1)

        username = StringVar()
        password = StringVar()

        e1 = Entry (register, textvariable=username)
        e2 = Entry (register, textvariable=password, show= "*")

        e1.grid(row = 0, column = 1)
        e2.grid(row = 1, column = 1)


        button1 = Button(register, text = "Register", command = combined_funcs(save, quit))
        button1.grid(columnspan = 2)
        button1.bind("<Button-1>")

        register.mainloop

1 个答案:

答案 0 :(得分:1)

绑定按钮时,回调函数应该是一个将事件作为参数的函数。 tkinter不知道你希望得到的用户名和密码,所以你不能绑定那种函数。

您需要创建一个回调函数,该函数获取用户输入的用户名和密码,然后使用这些参数调用leftClick函数。

还使用绑定到&lt; Button-1&gt;没有必要,因为您不需要知道点击的坐标。您的代码中已经有command = save,因此在按下按钮时应调用函数save

您可能想要这样的东西,因为您已经将Entry小部件绑定到变量,因此它们会自动填充:

def clickLogin():
        f = open('godhelpme.txt', 'r')
        file = open('some_file.txt', 'r')
        if username == (f.read()):
                if password == (file.read()):
                        print ("HELLOOOO")
                else:
                        print ("invalid username/password")
        else: print ("invalid username/password")


button2 = Button(login, text = "Log In", command = clickLogin)
button2.grid(columnspan = 2)

并且没有调用绑定按钮上的任何其他内容。