2014-06-24 6 views
1

J'ai codé en python avant mais je n'ai pas beaucoup utilisé tkinter. J'ai écrit ce code et je n'arrive pas à comprendre pourquoi ça ne marche pas. J'ai vérifié sur internet et n'ai rien trouvé. Cela s'est passé avant mais malheureusement je ne peux pas trouver le programme.Tkinter canvas widget

c'est le 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 



for i in range(3): 
    class Child(ball): 
     pass 
    childball = Child() 
    childball.start() 
    time.sleep(1) 

il revient sans cesse différentes erreurs à chaque fois par exemple

Exception in thread Thread-1: 
Traceback (most recent 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 option "35.0 34.0 45.0 44.0": must be 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 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" 

et

Exception in thread Thread-3: 
Traceback (most recent 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> 
    return [getdouble(x) for x in 
ValueError: could not convert string to float: 'coords' 

quelqu'un peut-il aider.

Répondre

0

Vous ne pouvez pas accéder aux widgets tkinter dans n'importe quel thread à l'exception de celui qui crée les widgets - tkinter n'est pas conçu pour fonctionner avec plusieurs threads.

Si tout ce que vous faites est de simples animations, vous n'avez pas besoin de threads. This answer donne un exemple d'animation avec la méthode after de tkinter.