Reputation: 384
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.
Upvotes: 0
Views: 353
Reputation: 143110
Try after
version but add something more then 0.0018
to playerY
and playerY2
.
Try at least 1
Working example:
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()
Upvotes: 1