python 3.3 - Tkinter canvas widget -
i have coded in python before haven't used tkinter much. have written code , can't figure out why isn't working. have check on net , haven't found anything. has happend before unfortunately can't find program.
this code
from tkinter import * import time import threading import random c = canvas() class ball(threading.thread): def run(self): x = 1 y = 1 ball = c.create_oval(0,0,10,10,fill = "green") c.pack() while true: c.move(ball,x,y) c.update() time.sleep(0.03) if c.coords(ball)[0] > 200: x = x - random.randint(1,2) if x > 2: x = 2 elif x < -2: x = -2 if c.coords(ball)[1] > 200: y = y - random.randint(1,2) if y > 2: y = 2 elif y < -2: y = -2 if c.coords(ball)[0] < 0: x = x + random.randint(1,2) if x > 2: x = 2 elif x < -2: x = -2 if c.coords(ball)[1] < 0: y = y + random.randint(1,2) if y > 2: y = 2 elif y < -2: y = -2 in range(3): class child(ball): pass childball = child() childball.start() time.sleep(1)
it keeps returning different errors each time example
exception in thread thread-1: traceback (most recent phone call last): file "c:\python33\lib\threading.py", line 901, in _bootstrap_inner self.run() file "c:/python33/game.py", line 35, in run if c.coords(ball)[0] < 0: file "c:\python33\lib\tkinter\__init__.py", line 2299, in coords self.tk.call((self._w, 'coords') + args))] _tkinter.tclerror: bad alternative "35.0 34.0 45.0 44.0": must addtag, bbox, bind, canvasx, canvasy, cget, configure, coords, create, dchars, delete, dtag, find, focus, gettags, icursor, index, insert, itemcget, itemconfigure, lower, move, postscript, raise, scale, scan, select, type, xview, or yview exception in thread thread-2: traceback (most recent phone call last): file "c:\python33\lib\threading.py", line 901, in _bootstrap_inner self.run() file "c:/python33/game.py", line 23, in run if c.coords(ball)[0] > 200: file "c:\python33\lib\tkinter\__init__.py", line 2299, in coords self.tk.call((self._w, 'coords') + args))] _tkinter.tclerror: invalid command name "89.0 49.0 99.0 59.0"
and
exception in thread thread-3: traceback (most recent phone call last): file "c:\python33\lib\threading.py", line 901, in _bootstrap_inner self.run() file "c:/python33/game.py", line 23, in run if c.coords(ball)[0] > 200: file "c:\python33\lib\tkinter\__init__.py", line 2299, in coords self.tk.call((self._w, 'coords') + args))] file "c:\python33\lib\tkinter\__init__.py", line 2297, in <listcomp> homecoming [getdouble(x) x in valueerror: not convert string float: 'coords'
can help.
you cannot access tkinter widgets in thread except 1 creates widgets -- tkinter isn't designed work multiple threads.
if you're doing simple animations, don't need threads. this answer gives illustration of doing animation tkinter's after
method.
tkinter python-3.3
No comments:
Post a Comment