2016-11-12 18 views
0
from tkinter import * 
import random 
import time 
tk = Tk() 
tk.title("Pong") 
tk.resizable(0,0) 
tk.wm_attributes("-topmost", 1) 
canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0) 
canvas.pack() 
tk.update() 
class Ball: 
    def _init_(self, canvas, color): 
     self.canvas = canvas 
     self.id = canvas.create_oval() 
     self.canvas.move(self.id, 245, 100) 

    def draw(self): 
     pass 
ball = Ball(canvas, 'red') 

當我運行的代碼,它會返回錯誤:對象有參數,但表示不

TypeError: object() takes no parameters 

即使對象的參數定義。

+4

'_init_'意味着什麼在Python特殊。每邊需要兩個下劃線 –

+1

你需要在每一邊有兩個下劃線'__init__' –

+0

@ cricket_007,這隻會帶來更多的錯誤 – user7148779

回答

3

正如其他人所指出的,你需要在__init__, 使用雙下劃線和你錯過參數​​電話:

def __init__(self, canvas, color): # <--- double underscores 
    self.canvas = canvas 
    self.id = canvas.create_oval(0, 0, 100, 100) # <--- missing arguments 
    self.canvas.move(self.id, 245, 100) 
相關問題