38

I have the code below:

from tkinter import *

class Window(Frame): 
    def __init__(self, master = None):
        Frame.__init__(self, master)
        self.master = master
        self.init_window()

    
    def init_window(self):
        self.master.title("COD:WWII Codes")
        self.pack(fill=BOTH, expand=1)
        codeButton = Button(
                        self, 
                        text="Generate Code", 
                        command=self.generatecode
                     )
        codeButton.place(x=0, y=0)

    def generatecode(self):
        f = open("C:/Programs/codes.txt", "r")
        t.insert(1.0. f.red())

root = Tk()
root.geometry("400x300")
app = Window(root)
root.mainloop()

Then, I got the error below:

TypeError: generatecode() takes 0 positional arguments but 1 was given

So, how can I solve the error?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Jason Martin
  • 578
  • 1
  • 4
  • 13
  • 4
    Please fix your indentation. You have 2 definitions of `generatecode()`. `self.generatecode()` is a bound method, meaning `self` will be passed as the first argument. – AChampion May 08 '17 at 04:06
  • Possible duplicate of [TypeError: worker() takes 0 positional arguments but 1 was given](https://stackoverflow.com/questions/18884782/typeerror-worker-takes-0-positional-arguments-but-1-was-given) – Alex Hall Aug 04 '18 at 14:30

3 Answers3

91

When you call a method on a class (such as generatecode() in this case), Python automatically passes self as the first argument to the function. So when you call self.my_func(), it's more like calling MyClass.my_func(self).

So when Python tells you "generatecode() takes 0 positional arguments but 1 was given", it's telling you that your method is set up to take no arguments, but the self argument is still being passed when the method is called, so in fact it is receiving one argument.

Adding self to your method definition should resolve the problem.

def generatecode(self):
    pass  # Do stuff here

Alternatively, you can make the method static, in which case Python will not pass self as the first argument:

@staticmethod
def generatecode():
    pass  # Do stuff here
Nikolas Stevenson-Molnar
  • 4,235
  • 1
  • 22
  • 31
  • 1
    thank you so much. this worked, now when i hit generate code it says t.insert is not defined? i posted the new code. – Jason Martin May 08 '17 at 04:18
  • Right. There is no variable `t` defined anywhere (also, `read` is misspelled on that line, and it looks like you have a dot instead of a comma so those will also cause a problems). – Nikolas Stevenson-Molnar May 08 '17 at 04:22
  • im just trying to output a single line from the text file into a textbox using the generatecode button, but im new to python and trying to look it up but everything i have tried isnt working. – Jason Martin May 08 '17 at 04:29
  • You'll need to reference that text box in your method. I'm not seeing where any text box is defined in the code you're showing here. – Nikolas Stevenson-Molnar May 08 '17 at 04:38
  • 1
    At any rate, it is probably better to open a new question as this one is resolved. And change your original question back to show the original code causing the error. Otherwise it's confusing to people reading this question (as the code you have there now would *not* produce the error. – Nikolas Stevenson-Molnar May 08 '17 at 04:39
  • 2
    after struggling for hours, I found you :} – 1UC1F3R616 Dec 24 '19 at 15:35
  • 2
    I tried 100 other ways too.. Now I could finish my code in peace all thanks to you and stackoverflow – Shravya Mutyapu Oct 30 '20 at 06:26
3

I got the same error:

TypeError: test() takes 0 positional arguments but 1 was given

When defining an instance method without self and I called it as shown below:

class Person:
          # ↓↓ Without "self"     
    def test(): 
        print("Test")

obj = Person()
obj.test() # Here

So, I put self to the instance method and called it:

class Person:
            # ↓↓ Put "self"     
    def test(self): 
        print("Test")

obj = Person()
obj.test() # Here

Then, the error was solved:

Test

In addition, even if defining an instance method with self, we cannot call it directly by class name as shown below:

class Person:
            # Here     
    def test(self): 
        print("Test")

Person.test() # Cannot call it directly by class name

Then, the error below occurs:

TypeError: test() missing 1 required positional argument: 'self'

But, if defining an instance method without self, we can call it directly by class name as shown below:

class Person:
          # ↓↓ Without "self"     
    def test(): 
        print("Test")

Person.test() # Can call it directly by class name

Then, we can get the result below without any errors:

Test

In detail, I explain about instance method in my answer for What is an "instance method" in Python? and also explain about @staticmethod and @classmethod in my answer for @classmethod vs @staticmethod in Python.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
0

The most upvoted answer does solve this issue,

And just in case anyone is doing this inside of a jupyternotebook. You must restart the kernel of the jupyternotebook in order for changes to update in the notebook