Python Forum
tkinter questions--- part 2 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: tkinter questions--- part 2 (/thread-14472.html)



tkinter questions--- part 2 - ironsheep - Dec-02-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??


RE: tkinter questions--- part 2 - woooee - Dec-02-2018

Tkinter requires a mainloop() which is an infinite loop that checks for events, places widgets on the screen, etc.


RE: tkinter questions--- part 2 - jfong - Dec-02-2018

first, line 10 should be
def __init__(self):
second, line 17 should be
position= canvas.coords(self.shape)


RE: tkinter questions--- part 2 - ironsheep - Dec-02-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???


RE: tkinter questions--- part 2 - jfong - Dec-03-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.


RE: tkinter questions--- part 2 - ironsheep - Dec-03-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?????


RE: tkinter questions--- part 2 - jfong - Dec-04-2018

The Traceback should tell you where the error is.
def__init__(self):


RE: tkinter questions--- part 2 - ironsheep - Dec-07-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


RE: tkinter questions--- part 2 - ironsheep - Dec-09-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