Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class takes no arguments
#1
Hello guys, I'm learning Python and there is a small problem in writing code for one game. I've created a Ball class with some attributes. And then, when I create a member of this class, I get a TypeError.
from tkinter import *
import random
import time
class Ball:
    def _init_(self, canvas, color):
        self.canvas = canvas
        self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
        self.canvas.move(self.id, 245, 100)
        
    def draw(self):
        pass
    
tk = Tk()
tk.title('Bounce!')
tk.resizable(0, 0)
tk.wm_attributes('-topmost', 1)
canvas = Canvas(tk, width=1800, height=1000, bd=1, highlightthickness=0)
canvas.pack()
tk.update()
ball = Ball(canvas, 'red')
while 1:
    tk.update_idletasks()
    tk.update()
    time.sleep(0.01)

Error:
Traceback (most recent call last): File "D:/Games/скок!.py", line 20, in <module> ball = Ball(canvas, 'red') TypeError: Ball() takes no arguments
Please, help me!
Reply
#2
class Ball:
    def _init_(self, canvas, color):
init should have double _
class Ball:
    def __init__(self, canvas, color):
Reply
#3
Thank you, Yoriz.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Class takes no arguments bily071 2 598 Oct-23-2023, 03:59 PM
Last Post: deanhystad
  Error TypeError: output_type_handler() takes 2 positional arguments but 6 were given paulo79 1 1,858 Oct-17-2022, 06:29 PM
Last Post: paulo79
  Checking the number of arguments a function takes Chirumer 3 2,113 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Class Takes No Arguments horuscope42 4 4,754 Oct-26-2020, 11:10 PM
Last Post: not_username1234
  Why Car() takes no arguments louis216 2 2,546 Jun-25-2020, 03:16 AM
Last Post: louis216
  random.choice() takes two positional arguments, but three were given. ShakeyPakey 5 11,488 May-31-2020, 03:13 PM
Last Post: deanhystad
  Question() takes no arguments jwrcfv 2 3,056 Apr-02-2020, 06:08 PM
Last Post: jwrcfv
  add() takes 2 positional arguments but 3 were given Man_from_India 3 5,699 Feb-10-2020, 05:08 PM
Last Post: Man_from_India
  Class takes no arguments Myang123 3 9,715 Nov-26-2019, 12:01 PM
Last Post: Davy_Jones_XIV
  This constructor takes no arguments Friend 2 5,270 Jun-26-2019, 02:54 PM
Last Post: Friend

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020