YusufChowdhury
YusufChowdhury

Reputation: 349

Error: "missing 5 required positional arguments" In Python, Tkinter

The specific error is:

_getandcreate() missing 5 required positional arguments: '_get_userpass', '_createuser', 't', 'username', and 'password'

Problematic subroutine:

def create_regwindow(self):
    t = tk.Toplevel(self)
    t.wm_title("Register")
    t.entry_user = tk.Entry(t)
    t.entry_pass = tk.Entry(t, show="*")
    t.title = tk.Label(t, text="Enter your new username and password below")
    t.field_user = tk.Label(t, text="Username")
    t.field_pass = tk.Label(t, text="Password")
    t.regbutton = tk.Button(t, text="Create Account", command=self._getandcreate) <-- HERE
    t.title.grid(row=0, sticky=tk.E)
    t.title.grid(row=0, column=1)
    t.field_user.grid(row=1, sticky=tk.E)
    t.field_pass.grid(row=2, sticky=tk.E)
    t.entry_user.grid(row=1, column=1)
    t.entry_pass.grid(row=2, column=1)
    t.regbutton.grid(row=3, column=1)

And the actual subroutine here:

def _getandcreate(self, _get_userpass, _createuser, t, username, password):
    _get_userpass(t)
    _createuser(username, password)

I need them variables to be passed into the command in the first block of code (labelled HERE), however, when I do this, I need to put these variables into a part of my code higher up (within init, whole code at bottom) - causing the issue of them not being defined.

I'm slightly confused. The purpose is, when the user clicks "Create Account", the data the user entered is taken and added to my database.

Upvotes: 1

Views: 1395

Answers (1)

Jean-Fran&#231;ois Fabre
Jean-Fran&#231;ois Fabre

Reputation: 140168

The callback system cannot know all your arguments. You could wrap your call in a lambda

t.regbutton = tk.Button(t, text="Create Account", command=self._getandcreate)

would become

t.regbutton = tk.Button(t, text="Create Account", command=lambda : self._getandcreate(t,<needed args>))

But there's much better to do here since you only deal with object members (methods or data).

Declare the _getandcreate and _get_userpass methods like this:

def _getandcreate(self):
    username, password = self._get_userpass()
    self._createuser(username, password)

def _get_userpass(self):
    t = self.__toplevel
    username = t.entry_user.get()
    password = t.entry_pass.get()
    return username, password

and set self.__toplevel in create_regwindow:

def create_regwindow(self):
    t = tk.Toplevel(self)
    self.__toplevel = t

Upvotes: 1

Related Questions