Tkinter 'bind'与 'canvas'和线程 ValueError [英] Tkinter 'bind' with 'canvas' and threading ValueError
问题描述
起初,我认为这是一个可以在this>我发现的帖子.但是,我尝试实现 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.
除非我遗漏了与 after
方法或线程模块有关的内容,否则我不明白如何解决这个问题.
旁注:我意识到我不应该在 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.
推荐答案
Try after
版本但添加比 0.0018
多的东西到 playerY
和playerY2
.
Try after
version but add something more then 0.0018
to playerY
and 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 &#39;bind&#39;与 &#39;canvas&#39;和线程 ValueError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!