Python Forum
tkinter questions--- part 2
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter questions--- part 2
#1
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??
Reply
#2
Tkinter requires a mainloop() which is an infinite loop that checks for events, places widgets on the screen, etc.
Reply
#3
first, line 10 should be
def __init__(self):
second, line 17 should be
position= canvas.coords(self.shape)
Reply
#4
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???
Reply
#5
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.
Reply
#6
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?????
Reply
#7
The Traceback should tell you where the error is.
def__init__(self):
Reply
#8
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
Reply
#9
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter questions--- part 1 ironsheep 4 2,992 Nov-27-2018, 03:34 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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