How to get PMW counter to call a function when value changes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DaveJ
    New Member
    • Nov 2011
    • 2

    How to get PMW counter to call a function when value changes

    Hi,
    I have implemented a number of megawidget counters, and I would like them to call a function when their values change. Is this possible?

    So far, I have only been able to get the counters to call my function when I enter keystrokes in the entryfield (excluding the up and down arrow keys, which do nothing). Here's a snippet:
    Code:
    myCounter.component('entryfield').bind("<Key>",myFunc)
    I would like the function to also be called when I click the uparrow and downarrow objects. However, if I do:
    Code:
    myCounter.component('uparrow').bind("<Button 1>",myFunc)
    then the uparrow loses its normal operation (I can put myCounter.incre ment() in myFunc, but when I click and hold the button, the counter only increments once, unlike the normal PMW counter behavior)

    I also tried using entryfield_comm and, but that only handles <Enter> key events.
    Code:
    myCounter = Pmw.Counter(myFrame,
        datatype = {'counter': 'real'},
        entryfield_validate = {'validator': 'real', 'min': 0.0, 'max': 10.0},
        entryfield_value = 2.0,
        entryfield_command = myFunc,
        entry_justify = 'center',
        entry_width = 4,
        orient = 'vertical',
        increment = 0.1,
        labelpos = 'w',
        label_text = 'My text')
    So, is there a way for the PMW counter to call a function when its value changes?

    Thanks in advance for your help
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Try this:
    Code:
    myCounter.component('uparrow').bind("<Button 1>",myFunc, add="+")

    Comment

    • DaveJ
      New Member
      • Nov 2011
      • 2

      #3
      Bvdet,
      Thank you; I'm extremely impressed! That works perfectly. Can you tell me how you figured that out so I know where to look in the future? I just ran help(Button.bin d), and it mentions add, but it doesn't say anything about the values that add can take. Thanks again.

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Check the Python documentation "Tkinter — Python interface to Tcl/Tk" section "24.1.6.7. Bindings and Events".

        Comment

        Working...