Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

ErrorCorner

ErrorCorner Logo ErrorCorner Logo

ErrorCorner Navigation

  • Home
  • Contact Us
  • About Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Contact Us
  • About Us
Home/ Questions/Q 598
Next
Answered
Kenil Vasani
Kenil Vasani

Kenil Vasani

  • 646 Questions
  • 567 Answers
  • 77 Best Answers
  • 26 Points
View Profile
  • 4
Kenil Vasani
Asked: December 14, 20202020-12-14T21:06:37+00:00 2020-12-14T21:06:37+00:00In: Python

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

  • 4

trying to create a program that when you hit the button(generate code) it extracts a line of data from a file and outputs into the

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

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()
pythontkinter
  • 1 1 Answer
  • 11 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    • Voted
    1. Kenil Vasani

      Kenil Vasani

      • 646 Questions
      • 567 Answers
      • 77 Best Answers
      • 26 Points
      View Profile
      Best Answer
      Kenil Vasani
      2020-12-14T21:02:23+00:00Added an answer on December 14, 2020 at 9:02 pm

      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
      
      • 9
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Sidebar

    Ask A Question
    • Popular
    • Kenil Vasani

      SyntaxError: invalid syntax to repo init in the AOSP code

      • 5 Answers
    • Kenil Vasani

      xlrd.biffh.XLRDError: Excel xlsx file; not supported

      • 3 Answers
    • Kenil Vasani

      Homebrew fails on MacOS Big Sur

      • 3 Answers
    • Kenil Vasani

      runtimeError: package fails to pass a sanity check for numpy ...

      • 3 Answers
    • Kenil Vasani

      Could not find tools.jar. Please check that /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home contains ...

      • 2 Answers

    Explore

    • Most Answered
    • Most Visited
    • Most Voted
    • Random

    © 2020-2021 ErrorCorner. All Rights Reserved
    by ErrorCorner.com