TheBeardedBerry
TheBeardedBerry

Reputation: 1764

Issues using self as a method argument in python

I apologize for the basic question but I am having some issues with either syntax or the overall concept I think. The problem I am having is that when I call the method in the class below the interpreter wants me to input a value for the self argument which I don't think is supposed to happen. What I am trying to do is create an object to hold several tkinter widgets at once so that I can dynamically add or subtract them in groups rather than one at a time. Any help here is greatly appreciated, thanks!

class User(object):

    #Input a UI row number and this will generate a corresponding row of widgets
    def generateLine(self, rowNumber):
        self.NameVar = StringVar()
        self.ReasonVar = StringVar()
        #ExcusedVar

        self.Name_Cbox = ec.AutocompleteCombobox(content, textvariable = self.NameVar)
        self.Name_Cbox.grid(row = rowNumber, column = 0)

        self.Reason_Cbox = ec.AutocompleteCombobox(content, textvariable = self.ReasonVar)
        self.Reason_Cbox.grid(row = rowNumber, column = 1)

Upvotes: 0

Views: 80

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122342

Make sure you have an instance of your User class to call the method on:

user = User()
user.generateLine(0)

self is only provided when the method has been bound to an instance.

If you call the method directly on the class, you'll get an exception:

>>> class User(object):
...     def generateLine(self, row):
...         print row
... 
>>> User.generateLine(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unbound method generateLine() must be called with User instance as first argument (got int instance instead)
>>> User().generateLine(0)
0

Upvotes: 3

Related Questions