0

In an attempt to make a program that takes a username and password given by the user and then loads that document, I've encountered an odd error load_user() missing 1 required positional argument: 'username' but it has been passed this by the user. I'll include the full code below, but the area where the issue may lie is the unfinished load function, I think:

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)

Like I say, the entire body of the class is included below just in case it is an issue in another area.

All help is appreciated, thanks!

EDIT: To clarify, I need the command to execute when the button is pressed, so passing self.load_user(...) doesn't work, as it executes when Tkinter opens.

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()
4lph
  • 21
  • 8
  • `command=self.load_user` calls `self.load_user()` when called. It needs an argument. `command=(lambda u=username: self.load_user(u))` – Steven Rumbalski May 06 '15 at 19:42

2 Answers2

3

The function has actually not been passed the username by the user. Your following code:

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

basically calls self.load_user() - with no arguments - when the load_game button is clicked. You can fix this in two ways:

  1. Turn the command into a lambda function:

load_game = Button(master, text="Load Game",
                   fg="black",command=lambda: self.load_user(username.get()))
  1. Eliminate the function's username parameter and simply have it access that global variable:

def load_user(self):
        print("...")
        global username
        jj = open("data_"+str(username.get())+".txt","r")
        ...
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
0
 command=self.load_user

Is your callback, but you're not passing any arguments to the function, even though it requires one.

davissandefur
  • 161
  • 1
  • 12