Tkinter'绑定'与'画布'和线程ValueError [英] Tkinter 'bind' with 'canvas' and threading ValueError
问题描述
起初,我认为这是一个可以在此中解决的问题我发现的一个帖子。但是,我尝试实现after方法,但似乎不起作用。 (有关我如何相信我的问题的更多信息,并且不是重复的内容在下方略有下降。)
At first, I thought this was an issue that could be solved in this post that I found. However, I have tried to implement the after method but it seems to not work. (More info on how I believe my question is different and not a duplicate is a little further down below.)
当试图将 button-1
绑定到函数 callback
,该函数是从其他线程运行的。有问题的代码在这里
The error is formed when trying to bind button-1
to the function callback
, which is running from a different thread than everything else. The code in question is here
def callback(event):
print(event) #This function normally changes playerY, however it prints the event for debugging purpose.
def drawPlayer():
global playerY, playerY2
player = canvas.create_oval(50,50,100,100,fill="yellow",outline="black")
while True:
canvas.coords(player,(50,50,100,100)) #This would usually use playerY and playerY2 but for debugging it does not.
playerY += 0.0018
playerY2 += 0.0018
root.bind("<Button-1>",callback)
thread2 = Thread(target=drawPlayer)
thread2.start()
显然,诸如root,playerY,playerY2等是已经定义,但我不会粘贴整个代码。
Obviously things such as root, playerY, playerY2 etc are already definded but I am not going to paste my whole code.
单击按钮1会产生以下回溯
When button 1 is clicked this will produce the following traceback
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Python33\lib\threading.py", line 637, in _bootstrap_inner
self.run()
File "C:\Python33\lib\threading.py", line 594, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Harvey\Documents\School Work\Computer Science\Tkinter\tkinterFallpyBird.py", line 58, in drawPlayer
canvas.coords(player,(50,50,100,100))
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: 'None'
另一个线程告诉我要使用'after'方法来解决这个问题。为此,我尝试了以下操作:
The other thread tells me to solve this by using the 'after' method. To do this I tried:
def drawPlayer():
global playerY, playerY2,player
canvas.coords(player,(playerY,50,playerY2,100))
playerY += 0.0018
playerY2 += 0.0018
root.after(1,drawPlayer)
root.bind("<Button-1>",callback)
player = canvas.create_oval(50,50,100,100,fill="yellow",outline="black")
drawPlayer()
root.mainloop()
我的函数 drawPlayer
需要重复调用,因此线程版本中的while循环。我觉得这就是我的问题与我一开始所链接的问题有何不同。我试图在 root.after()
调用中使用 0
,但这只会导致调用后的代码 drawPlayer()
未运行。
My function drawPlayer
needs to be called repeatedly, hence the while loop in the threaded version. I feel this is how my question differs from the one I linked at the start. I tried to use 0
in the root.after()
call, but that just leads to code after the call of drawPlayer()
not being run.
除非,我缺少与之后
方法或线程模块,我不知道如何解决此问题。
旁注:我意识到我不应该在tkinter上开发一款游戏,尤其是需要同时进行多件事的游戏。但是,我在学校正在做这个事情,我想使用的模块(Pygame或Pyglet)无法下载,仅仅是为了我做一个没有真正目的的游戏。如果我可以使用tkinter以外的其他方式,我可能会使用。感谢您的帮助。
Unless, I am missing something to do with the after
method, or the threading module, I don't understand how I can fix this issue.
Side note: I realise I should not be making a game in tkinter, especially one that requires multiple things to be happening at once. However, I am doing this at school and the modules I would like to use (Pygame or Pyglet) cannot be downloaded just for me to make a game that has no real purpose. If I could use something other than tkinter I probably would. Thank you for your help.
推荐答案
在版本之后尝试
,但再添加
0.0018
到 playerY
和 playerY2
。
尝试至少 1
工作示例:
from Tkinter import *
root = Tk()
canvas = Canvas(root)
canvas.pack()
playerY = 50
playerY2 = 100
def drawPlayer():
global playerY, playerY2,player
canvas.coords(player,(playerY,50,playerY2,100))
playerY += 1
playerY2 += 1
root.after(10,drawPlayer)
#root.bind("<Button-1>",callback)
player = canvas.create_oval(50,50,100,100,fill="yellow",outline="black")
drawPlayer()
root.mainloop()
这篇关于Tkinter'绑定'与'画布'和线程ValueError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!