Tkinter: How to bind an event that repeats if continued to be selected

Don Arnold darnold02 at sprynet.com
Thu Jan 10 07:01:40 EST 2002


On  8-Jan-2002, Martyn Quick <mrq at for.mat.bham.ac.uk> wrote:

> Hi All!
>
> The following appears to be a bit difficult, but probably it isn't if you
> are an expert.
>
> I've created a `counter'-like widget as shown below.  Basically if you
> click on the arrow button it increases the value displayed in the entry.
>
> My question is what do I have to change in the bindings to make it behave
> slightly differently, namely if you click on the arrow button and hold,
> then after a while it begins to automatically increase (i.e., repeatedly
> call  self.increment  until you release?
>
> Many thanks in advance for any help!
>
> Yours,
>
> Martyn Quick
>
> --------------CODE FOLLOWS-------------------
>
> import Pmw, Tkinter
>
> class Counter(Pmw.MegaWidget):
>
>     def __init__(self, parent=None):
>         Pmw.MegaWidget.__init__(self, parent)
>         interior = self.interior()
>         self.var = Tkinter.IntVar()
>         self.createcomponent(
>             'entry', (), None, Tkinter.Entry, (interior,),
>             state='disabled', textvariable=self.var,
>             width=5).pack(side='left')
>         self.arrow = self.createcomponent(
>             'arrow', (), None, Tkinter.Canvas, (interior,), width=16,
>             height=16, relief='raised', borderwidth=1)
>         self.arrow.pack()
>         Pmw.drawarrow(self.arrow, 'black', 'up', 'arrow')
>         self.arrow.bind('<1>', self.increment)
>         self.arrow.bind('<Any-ButtonRelease-1>', self.release)
>
>     def increment(self, event):
>         self.arrow.config(relief='sunken')
>         self.var.set(self.var.get()+1)
>
>     def release(self, event):
>         self.arrow.config(relief='raised')
>
> --------------------------------------------------------
> Dr. Martyn Quick  (Lecturer in Pure Mathematics)
> University of Birmingham, Edgbaston, Birmingham, UK.
> http://www.mat.bham.ac.uk/M.R.Quick/


Doesn't Pmw already offer a Counter class that has this functionality? I
know the version I have at home (whichever it might be) does.



More information about the Python-list mailing list