Python 移动函数Tkinter出错
我在windows中使用Python3.4.1,当我关闭Tkinter时,我得到一条关于move Tkinter函数的错误消息。应用程序工作正常,但当我关闭应用程序时,shell会向我报告以下错误消息:Python 移动函数Tkinter出错,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我在windows中使用Python3.4.1,当我关闭Tkinter时,我得到一条关于move Tkinter函数的错误消息。应用程序工作正常,但当我关闭应用程序时,shell会向我报告以下错误消息: C:\Python34\python.exe "E:/ProgramasPython3/Programas TKinter/JuegoPong/Pong.py" Traceback (most recent call last): File "E:/ProgramasPython3/Prog
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
C:\Python34\python.exe“E:/ProgramasPython3/Programas TKinter/JuegoPong/Pong.py”
回溯(最近一次呼叫最后一次):
文件“E:/ProgramasPython3/Programas TKinter/JuegoPong/Pong.py”,第96行,in
main()
文件“E:/ProgramasPython3/Programas TKinter/JuegoPong/Pong.py”,第89行,主目录
pelota.dibujar()
文件“E:/ProgramasPython3/ProgramasTkinter/JuegoPong/Pong.py”,第35行,在dibujar中
self.canvas.move(self.id、self.x、self.y)
文件“C:\Python34\lib\tkinter\\uuuu init\uuuuu.py”,第2398行,移动中
self.tk.call((self._w,'move')+args)
_tkinter.TclError:无效的命令名“.42952128”
进程已完成,退出代码为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()
#/usr/bin/env蟒蛇34
从tkinter导入(Tk、框架、画布)
随机输入
将时间导入为时间
佩洛塔级(框架):
定义初始(自我、画布、拉克塔、颜色):
帧。\uuuu初始化(self,master=None)
self.canvas=画布
self.raqueta=raqueta
self.color=颜色
self.id=self.canvas.create_oval(10.0,10.0,25.0,25.0,fill=self.color)
self.canvas.move(self.id,245.01000.0)
empezar=[-3,-2,-1,0,1,2,3]
随机。洗牌(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(自我,职位):
raqueta_pos=self.canvas.coords(self.raqueta.id)
如果pos[2]>=raqueta_pos[0]和pos[0]我想你可以找到一个更好的方法,但是尝试一下/except不会有任何错误
while 1:
try:
pelota.dibujar()
raqueta.dibujar()
tk.update_idletasks()
tk.update()
time.sleep(0.01)
except Exception as e:
break
最后的while
循环尝试在处理GUI元素后执行另一次更新。@tobias_k如果我对更新进行注释,程序将无法工作,并且无法解决问题。不要对整个循环进行注释。一个快速而肮脏的解决方案就是将该循环放入一个try/except
,这样就不会显示错误。毕竟,正如你所说,它仍然有效。一个更干净的解决方案是去掉这个循环,使用tkinter小部件的after
方法,让它们自己更新。