2014-07-16 56 views
1

我使用python 3.4.1与Windows和当我关闭我的Tkinter时,我获得有关移动tkinter函数的错误消息。该应用程序工作正常,但是当我关闭我的应用程序的壳报道了我这个错误消息:错误与移动功能Tkinter

C:\Python34\python.exe "E:/ProgramasPython3/Programas TKinter/JuegoPong/Pong.py" 
Traceback (most recent call last): 
    File "E:/ProgramasPython3/Programas TKinter/JuegoPong/Pong.py", line 96, in <module> 
    main() 
    File "E:/ProgramasPython3/Programas TKinter/JuegoPong/Pong.py", line 89, in main 
    pelota.dibujar() 
    File "E:/ProgramasPython3/Programas TKinter/JuegoPong/Pong.py", line 35, in dibujar 
    self.canvas.move(self.id, self.x, self.y) 
    File "C:\Python34\lib\tkinter\__init__.py", line 2398, in move 
    self.tk.call((self._w, 'move') + args) 
_tkinter.TclError: invalid command name ".42952128" 

Process finished with exit code 1 

这是我的代码:

#!/usr/bin/env python34 

from tkinter import (Tk, Frame, Canvas) 
import random 
import time as time 


class Pelota(Frame): 
    def __init__(self, canvas, raqueta, color): 
     Frame.__init__(self, master=None) 
     self.canvas = canvas 
     self.raqueta = raqueta 
     self.color = color 
     self.id = self.canvas.create_oval(10.0, 10.0, 25.0, 25.0, fill=self.color) 
     self.canvas.move(self.id, 245.0, 100.0) 
     empezar = [-3, -2, -1, 0, 1, 2, 3] 
     random.shuffle(empezar) 
     self.x = empezar[0] 
     self.y = -3.0 
     self.canvas_height = self.canvas.winfo_reqheight() 
     self.canvas_width = self.canvas.winfo_reqwidth() 

    def golpea_raqueta(self, pos): 
     raqueta_pos = self.canvas.coords(self.raqueta.id) 
     if pos[2] >= raqueta_pos[0] and pos[0] <= raqueta_pos[2]: 
      if raqueta_pos[1] <= pos[3] <= raqueta_pos[3]: 
       return True 
     return False 

    def dibujar(self): 
     self.canvas.move(self.id, self.x, self.y) 
     pos = self.canvas.coords(self.id) 
     if pos[1] <= 0.0: 
      self.y = 3.0 
     elif pos[3] >= self.canvas_height: 
      self.y = -3.0 
     elif self.golpea_raqueta(pos): 
      self.y = -3.0 
     elif pos[0] <= 0.0: 
      self.x = 3.0 
     elif pos[2] >= self.canvas_width: 
      self.x = -3.0 


class Raqueta(Frame): 
    def __init__(self, canvas, color): 
     Frame.__init__(self, master=None) 
     self.canvas = canvas 
     self.color = color 
     self.x = 0.0 
     self.id = self.canvas.create_rectangle(0.0, 0.0, 100.0, 10.0, fill=self.color) 
     self.canvas.move(self.id, 200.0, 300.0) 
     self.canvas_width = self.canvas.winfo_reqwidth() 
     self.canvas.bind_all('<KeyPress-Left>', self.to_left) 
     self.canvas.bind_all('<KeyPress-Right>', self.to_right) 

    def dibujar(self): 
     self.canvas.move(self.id, self.x, 0.0) 
     pos = self.canvas.coords(self.id) 
     if pos[0] <= 0.0: 
      self.x = 0.0 
     elif pos[2] >= self.canvas_width: 
      self.x = 0.0 

    def to_left(self, evt): 
     self.x = -2.0 

    def to_right(self, evt): 
     self.x = 2.0 


def main(): 
    tk = Tk() 

    tk.title('Mi Pong') 

    canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0) 
    raqueta = Raqueta(canvas, 'blue') 
    pelota = Pelota(canvas, raqueta, 'red') 
    tk.resizable(0, 0) 
    tk.wm_attributes('-topmost', 1) 
    tk.update() 
    canvas.pack() 
    while 1: 
     pelota.dibujar() 
     raqueta.dibujar() 
     tk.update_idletasks() 
     tk.update() 
     time.sleep(0.01) 


main() 

应用程序是一个简单的乒乓球比赛。

回力球是球 Raqueta是网球拍 dibujar是情节 golpea raqueta是敲网球拍

感谢

+0

在处理完GUI元素之后,'while'循环会尝试执行另一次更新。 –

+0

@tobias_k如果我评论更新程序不起作用,并不能解决问题。 – Tobal

+1

不要评论整个循环。一个快速和肮脏的解决方案将只是把这个循环放入一个'try/except',所以错误不会被显示出来。毕竟,如你所说,它仍然有效。一个更清洁的解决方案是取消该循环,并使用tkinter小部件的'after'方法来让它们自己更新。 –

回答

1

我想你可以找到一个更好的方式,但把它放在一个try /除外将结束没有任何错误。

 while 1: 
     try: 
      pelota.dibujar() 
      raqueta.dibujar() 
      tk.update_idletasks() 
      tk.update() 
      time.sleep(0.01) 
     except Exception as e: 
      break