Tkinter binding and keyboard hardware settings (WinXP)

A

apriebe47

Hello,

I have created a simple canvas in Tkinter to display a number of
PhotoImages, I then bind a key (in my case, <Up>) to start a loop that
plays through a list of PhotoImages to make it an animation of sorts.
What I noticed is, after holding down the key for a certain time, it
would not loop through all 6 animations, instead it would just go
through the first 2 and reset it self. I changed the key bind to
Button-1 and did not encounter this problem. I then went into my
keyboard hardware settings and changed the repeat rate to the longest
delay and the repeat delay to the longest delay. This improved it some
what, but I don't think it is practical to do this everytime I start up
the program. My question is, is there a way to bind a key so as to make
it ignore the keyboard's delay and repeat rate settings? Here's the
code just so you can get an idea:

# GIF Animation Test

from Tkinter import *
from time import sleep

class GIF:
def __init__(self, root):
self.root = root
self.canvas = Canvas(root, bg="white", width=225, height=200)
self.canvas.pack()
self.cronoUpImageList =[
"cronoup1.gif", "cronoup2.gif", "cronoup3.gif",
"cronoup4.gif", "cronoup5.gif", "cronoup6.gif"]
self.gifImage = PhotoImage(file=self.cronoUpImageList[0])
self.cronoDefUp = PhotoImage(file="cronodefup.gif")
self.drawImage = self.canvas.create_image(112.5, 100, image=
self.cronoDefUp)
self.canvas.bind("<Up>", self.keyboardPrsUp)
self.canvas.bind("<Down>", self.keyboardRlsUp)

def keyboardPrsUp(self, event):
self.canvas.delete(self.drawImage)
self.counter = 0
self.killEvent = False
while self.killEvent != True:
print self.cronoUpImageList[self.counter]
self.gifImage =
PhotoImage(file=self.cronoUpImageList[self.counter])
self.drawImage = self.canvas.create_image(112.5, 100,
image=
self.gifImage)
self.counter = self.counter + 1
if self.counter > 5:
self.counter = 0
self.canvas.update()
sleep(.05)


def keyboardRlsUp(self, event):
self.counter = 0
self.killEvent = True
self.canvas.delete(self.drawImage)
self.drawImage = self.canvas.create_image(112.5, 100, image=
self.cronoDefUp)


root = Tk()
root.title("GIF Animation Test")
app = GIF(root)
root.mainloop()
 
C

Claudio Grondi

Hello,

I have created a simple canvas in Tkinter to display a number of
PhotoImages, I then bind a key (in my case, <Up>) to start a loop that
plays through a list of PhotoImages to make it an animation of sorts.
What I noticed is, after holding down the key for a certain time, it
would not loop through all 6 animations, instead it would just go
through the first 2 and reset it self. I changed the key bind to
Button-1 and did not encounter this problem. I then went into my
keyboard hardware settings and changed the repeat rate to the longest
delay and the repeat delay to the longest delay. This improved it some
what, but I don't think it is practical to do this everytime I start up
the program. My question is, is there a way to bind a key so as to make
it ignore the keyboard's delay and repeat rate settings? Here's the
code just so you can get an idea:

# GIF Animation Test

from Tkinter import *
from time import sleep

class GIF:
def __init__(self, root):
self.root = root
self.canvas = Canvas(root, bg="white", width=225, height=200)
self.canvas.pack()
self.cronoUpImageList =[
"cronoup1.gif", "cronoup2.gif", "cronoup3.gif",
"cronoup4.gif", "cronoup5.gif", "cronoup6.gif"]
self.gifImage = PhotoImage(file=self.cronoUpImageList[0])
self.cronoDefUp = PhotoImage(file="cronodefup.gif")
self.drawImage = self.canvas.create_image(112.5, 100, image=
self.cronoDefUp)
self.canvas.bind("<Up>", self.keyboardPrsUp)
self.canvas.bind("<Down>", self.keyboardRlsUp)

def keyboardPrsUp(self, event):

currentTime = ...
if( currentTime - self.timeOfLastKeyPush < self.minDelay):
pass
else:
self.timeOfLastKeyPush = currentTime
...
self.canvas.delete(self.drawImage)
self.counter = 0
self.killEvent = False
while self.killEvent != True:
print self.cronoUpImageList[self.counter]
self.gifImage =
PhotoImage(file=self.cronoUpImageList[self.counter])
self.drawImage = self.canvas.create_image(112.5, 100,
image=
self.gifImage)
self.counter = self.counter + 1
if self.counter > 5:
self.counter = 0
self.canvas.update()
sleep(.05)


def keyboardRlsUp(self, event):
self.counter = 0
self.killEvent = True
self.canvas.delete(self.drawImage)
self.drawImage = self.canvas.create_image(112.5, 100, image=
self.cronoDefUp)


root = Tk()
root.title("GIF Animation Test")
app = GIF(root)
root.mainloop()

What about simple tracking of time (see above NOT tested rough draft) in
the keyboardPrsUp and keyboardRlsUp functions, so, that events raised
earlier than after a specified delay period will just become ignored?

Claudio Grondi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top