Tkinter binding and keyboard hardware settings (WinXP)

Discussion in 'Python' started by apriebe47@gmail.com, Jul 10, 2006.

  1. Guest

    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()
     
    , Jul 10, 2006
    #1
    1. Advertising

  2. wrote:
    > 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
     
    Claudio Grondi, Jul 10, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Siegfried Heintze
    Replies:
    0
    Views:
    2,282
    Siegfried Heintze
    Oct 24, 2005
  2. CSharpner
    Replies:
    0
    Views:
    1,052
    CSharpner
    Apr 9, 2007
  3. leeanne
    Replies:
    0
    Views:
    1,878
    leeanne
    Sep 24, 2008
  4. Replies:
    0
    Views:
    337
  5. donet programmer
    Replies:
    3
    Views:
    1,555
    Gregory A. Beamer
    Nov 20, 2009
Loading...

Share This Page