【问题标题】:Tkinter canvas widgetTkinter 画布小部件
【发布时间】:2014-06-24 20:41:39
【问题描述】:

我以前用 python 编写过代码,但我没有太多使用 tkinter。我已经编写了这段代码,但我无法弄清楚它为什么不起作用。我已经在互联网上检查并没有找到任何东西。以前发生过这种情况,但不幸的是我找不到该程序。

这是代码

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)

例如,它每次都会返回不同的错误

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"

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'

谁能帮忙。

【问题讨论】:

    标签: tkinter python-3.3


    【解决方案1】:

    除了创建小部件的线程之外,您无法在任何线程中访问 tkinter 小部件——tkinter 并非设计用于多线程。

    如果您所做的只是简单的动画,则不需要线程。 This answer 给出了一个使用 tkinter 的 after 方法做动画的例子。

    【讨论】:

      猜你喜欢
      • 2019-08-07
      • 1970-01-01
      • 2018-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-23
      相关资源
      最近更新 更多