Tkinter按钮回调缺少它包含的参数

时间:2015-05-06 19:34:28

标签: python tkinter loading typeerror

尝试制作一个程序,该程序使用用户提供的用户名和密码,然后加载该文档,我遇到了一个奇怪的错误     load_user() missing 1 required positional argument: 'username'但是用户已将其传递给了它。我将在下面提供完整的代码,但问题所在的区域是未完成的加载功能,我认为:

def load_user(self,username):
    print("...")
    jj = open("data_"+str(username)+".txt","r")
    lines = jj.readlines()
    user = str(lines[2])
    passw = str(lines[4])
    print("...")
    if password == passw:
        print("Loaded save of "+user)

就像我说的那样,下面包括整个课程,以防它在另一个领域出现问题。

感谢所有帮助,谢谢!

编辑:为了澄清,我需要在按下按钮时执行命令,因此传递self.load_user(...)不起作用,因为它在Tkinter打开时执行

from tkinter import *
get = 0

class Menu_F:
    def __init__(self,master):

        frame = Frame(master)
        frame.grid()

        title_username = Label(master, text="User Name")
        title_username.grid(row=0, column=0)

        title_password = Label(master, text="Password")
        title_password.grid(row=1, column=0)

        global username
        username = Entry(master, bd=5)
        username.grid(row=0, column=1)

        global password
        password = Entry(master, bd=5, show="*")
        password.grid(row=1, column=1)

        load_game = Button(master, text="Load Game",
                           fg="black",command=self.load_user)
        load_game.grid(row=3, column=1)

        new_game = Button(master, text="New Game",
                          fg="black",command=self.new_user)
        new_game.grid(row=3, column=0)

        confirm_username = Button(master, text="Done",
                                  fg="black",command=self.get_username)
        confirm_username.grid(row=0, column=2)

        confirm_password = Button(master, text="Done",
                                  fg="black",command=self.get_password)
        confirm_password.grid(row=1, column=2)

    def get_username(self):
        get = 1
        global username
        username = username.get()
        print("Username is: "+username)

    def get_password(self):
        global password
        password = password.get()
        print("Password is: "+password)

    def load_user(self,username):
        print("...")
        jj = open("data_"+str(username)+".txt","r")
        lines = jj.readlines()
        user = str(lines[2])
        passw = str(lines[4])
        print("...")
        if password == passw:
            print("Loaded save of "+user)

    def save_user(self,username,password,progress):
        ss = open("data_"+str(username)+".txt","w")
        ss.write("""_-_-_
-_-_-
"""+str(username)+"""
_-_-_
"""+str(password)+"""
-_-_-
"""+int(progress))

    def new_user(self,username,password):
        nn = open("data_"+str(username)+".txt","w")
        nn.write("""_-_-_
-_-_-
"""+str(username)+"""
_-_-_
"""+str(password)+"""
-_-_-
0""")

root = Tk()
root.resizable(width=FALSE, height=FALSE)
app = Menu_F(root)
mainloop()

2 个答案:

答案 0 :(得分:2)

该功能实际上没有被用户传递用户名。您的以下代码:

load_game = Button(master, text="Load Game",
                   fg="black",command=self.load_user)

基本上调用self.load_user() - 没有参数 - 单击load_game按钮时。您可以通过两种方式解决此问题:

  1. command变为lambda功能:
  2. load_game = Button(master, text="Load Game",
                       fg="black",command=lambda: self.load_user(username.get()))
    
    1. 消除函数的username参数,只需让它访问该全局变量:
    2. def load_user(self):
              print("...")
              global username
              jj = open("data_"+str(username.get())+".txt","r")
              ...
      

答案 1 :(得分:0)

 command=self.load_user

是你的回调,但是你没有向函数传递任何参数,即使它需要一个。