Posts: 22
Threads: 5
Joined: Oct 2018
from tkinter import *
import random
import time
tk=Tk()
canvas=Canvas(tk, width=500, height=400)
canvas.pack()
class Ball:
def init(self):
self.shape = canvas.create_oval(10,10,60,60,fill="red")
self.xspeed= 3
self.yspeed= 4
def move(self):
canvas.move(self.shape, self.xspeed, self.yspeed)
position= canvas.coords(ball)
if position[3]>= 400 or position[1] <= 0:
self.yspeed= -self.yspeed
if position[2] >= 500 or position[0] <= 0:
self.xspeed= -self.xspeed
ball1= Ball()
while True:
ball1.move()
tk.update()
time.sleep(.01)
I keep getting the Error: Traceback (most recent call last):
File "C:\Users\Mitesh Patel\Desktop\trash.py", line 26, in <module>
ball1.move()
File "C:\Users\Mitesh Patel\Desktop\trash.py", line 16, in move
canvas.move(self.shape, self.xspeed, self.yspeed)
AttributeError: 'Ball' object has no attribute 'shape'
>>>
Why won't a ball oval shaped appear and move around on my screen??
Posts: 536
Threads: 0
Joined: Feb 2018
Dec-02-2018, 06:41 AM
(This post was last modified: Dec-02-2018, 06:41 AM by woooee.)
Tkinter requires a mainloop() which is an infinite loop that checks for events, places widgets on the screen, etc.
Posts: 29
Threads: 0
Joined: Jul 2018
first, line 10 should be
def __init__(self):
second, line 17 should be
position= canvas.coords(self.shape)
Posts: 22
Threads: 5
Joined: Oct 2018
I still don't get it---
from tkinter import *
import random
import time
tk=Tk()
canvas=Canvas(tk, width=500, height=400)
canvas.pack()
class Ball:
def _init_(self):
self.shape = canvas.create_oval(10,10,60,60,fill="red")
self.xspeed= 3
self.yspeed= 4
def move(self):
canvas.move(self.shape, self.xspeed, self.yspeed)
position= canvas.coords(self.shape)
if position[3]>= 400 or position[1] <= 0:
self.yspeed= -self.yspeed
if position[2] >= 500 or position[0] <= 0:
self.xspeed= -self.xspeed
ball1 = Ball()
while True:
ball1.move()
tk.update()
time.sleep(.01)
tk.mainloop() Error: Traceback (most recent call last):
File "C:\Users\Mitesh Patel\Desktop\trash.py", line 26, in <module>
ball1.move()
File "C:\Users\Mitesh Patel\Desktop\trash.py", line 16, in move
canvas.move(self.shape, self.xspeed, self.yspeed)
AttributeError: 'Ball' object has no attribute 'shape'
Ball1 is obviously a ball--- how can the computer not get that???
Posts: 29
Threads: 0
Joined: Jul 2018
It's double underscore, __init__()
By the way, the last line tk.mainloop() is not required. In your codes, the while loop works like it.
Posts: 22
Threads: 5
Joined: Oct 2018
from tkinter import *
import random
import time
tk=Tk()
canvas=Canvas(tk, width=500, height=400)
canvas.pack()
class Ball:
def__init__(self):
self.shape = canvas.create_oval(10,10,60,60,fill="red")
self.xspeed= 3
self.yspeed= 4
def move(self):
canvas.move(self.shape, self.xspeed, self.yspeed)
position= canvas.coords(self.shape)
if position[3]>= 400 or position[1] <= 0:
self.yspeed= -self.yspeed
if position[2] >= 500 or position[0] <= 0:
self.xspeed= -self.xspeed
ball1 = Ball()
while True:
ball1.move()
tk.update()
time.sleep(.01)
Now I keep getting invalid syntax?????
Posts: 29
Threads: 0
Joined: Jul 2018
The Traceback should tell you where the error is.
def__init__(self):
Posts: 22
Threads: 5
Joined: Oct 2018
from tkinter import *
import random
import time
tk=Tk()
canvas=Canvas(tk, width=500, height=400)
canvas.pack()
class Ball:
def init_(self):
self.shape = canvas.create_oval(10,10,60,60,fill="red")
self.xspeed= 3
self.yspeed= 4
def move(self):
canvas.move(self.shape, self.xspeed, self.yspeed)
position= canvas.coords(ball)
if position[3]>= 400 or position[1] <= 0:
self.yspeed= -self.yspeed
if position[2] >= 500 or position[0] <= 0:
self.xspeed= -self.xspeed
ball1= Ball()
while True:
ball1.move()
tk.update()
time.sleep(.01)
I still don't get it
Posts: 22
Threads: 5
Joined: Oct 2018
from tkinter import *
import random
import time
tk=Tk()
canvas=Canvas(tk, width=500, height=400)
canvas.pack()
class Ball:
def__init__(self):
self.shape = canvas.create_oval(10,10,60,60,fill="red")
self.xspeed= 3
self.yspeed= 4
def move(self):
canvas.move(self.shape, self.xspeed, self.yspeed)
position= canvas.coords(ball)
if position[3]>= 400 or position[1] <= 0:
self.yspeed= -self.yspeed
if position[2] >= 500 or position[0] <= 0:
self.xspeed= -self.xspeed
ball1= Ball()
while True:
ball1.move()
tk.update()
time.sleep(.01)
I do double underscore and I still get syntax error
|