jeudi 3 juillet 2014

Tkinter 'bind' with 'canvas' and threading ValueError


Vote count:

0




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.)


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()


Obviously things such as root, playerY, playerY2 etc are already definded but I am not going to paste my whole code.


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'


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()


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.


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.

asked 1 min ago

Harvey

198





Aucun commentaire:

Enregistrer un commentaire