EVT_SPIN_UP and EVT_SPIN_DOWN events don't work with wx.SpinButton

Hi,

I’m trying to bind some functions when up/down arrow buttons are clicked on a wx.SpinButton widget. I tried the following but nothing happens when I click up/down arrow button, what is wrong here:

import wx
import wx.lib.masked as masked

···

########################################################################
class MyPanel(wx.Panel):
“”""""

#----------------------------------------------------------------------
def __init__(self, parent):
    """Constructor"""
    wx.Panel.__init__(self, parent)

    self.mainSizer = wx.BoxSizer(wx.VERTICAL)

    text1 = wx.StaticText( self, -1, "12-hour format:", size=(150,-1))
    self.time12 = masked.TimeCtrl( self, -1, name="12 hour control" )
    h = self.time12.GetSize().height
    print "h is: ", h
    spin1 = wx.SpinButton( self, -1, wx.DefaultPosition, (-1,h), wx.SP_VERTICAL )
    self.time12.BindSpinButton( spin1 )
    self.addWidgets([text1, self.time12, spin1])

    text2 = wx.StaticText( self, -1, "24-hour format:")
    self.spin2 = wx.SpinButton( self, -1, wx.DefaultPosition, (-1,h), wx.SP_VERTICAL )
    self.spin2.Bind(wx.EVT_SPIN_UP, self.on_spin_up)
    self.spin2.Bind(wx.EVT_SPIN_DOWN, self.on_spin_down)
   
    self.time24 = masked.TimeCtrl(
                    self, -1, name="24 hour control", fmt24hr=True,
                    spinButton = self.spin2
                    )
    self.addWidgets([text2, self.time24, self.spin2])

    text3 = wx.StaticText( self, -1, "No seconds\nor spin button:")
    self.spinless_ctrl = masked.TimeCtrl(
                            self, -1, name="spinless control",
                            display_seconds = False
                            )
    self.addWidgets([text3, self.spinless_ctrl])

    self.SetSizer(self.mainSizer)

#----------------------------------------------------------------------
def addWidgets(self, widgets):
    """"""
    sizer = wx.BoxSizer(wx.HORIZONTAL)
    for widget in widgets:
        if isinstance(widget, wx.StaticText):
            sizer.Add(widget, 0, wx.ALL|wx.CENTER, 5),
        else:
            sizer.Add(widget, 0, wx.ALL, 5)
    self.mainSizer.Add(sizer)
   
def on_spin_up(self, event):
    print "up pressed"
    print self.spin2.GetValue()
    event.Skip()
   
def on_spin_down(self, event):
    print "down pressed"
    print self.spin2.GetValue()
    event.Skip()       

########################################################################
class MyFrame(wx.Frame):
“”""""

#----------------------------------------------------------------------
def __init__(self):
    """Constructor"""
    wx.Frame.__init__(self, None, title="Spinner Demo")
    panel = MyPanel(self)
    self.Show()

if name == “main”:
app = wx.App(False)
f = MyFrame()
app.MainLoop()

Hi Steve,

The below works.
I modified Mike Driscoll’s wx.SpinButton example to handle spin up and down events.
See: http://www.blog.pythonlibrary.org/2010/06/10/wxpython-a-tour-of-buttons-part-2-of-2/
Yes, I am aware there is a mixture of old and new binding methods.

Bruce

import wx

class MyForm(wx.Frame):

def __init__(self):
    wx.Frame.__init__(self, None, wx.ID_ANY, "Spin Button Tutorial")
    panel = wx.Panel(self, wx.ID_ANY)

    self.text = wx.TextCtrl(panel, value="1")
    self.spin = wx.SpinButton(panel, style=wx.SP_VERTICAL)
    self.spin.SetRange(1, 100)
    self.spin.SetValue(1)

    self.Bind(wx.EVT_SPIN, self.OnSpin, self.spin)
   
    self.spin.Bind(wx.EVT_SPIN_UP, self.on_spin_up)
    self.spin.Bind(wx.EVT_SPIN_DOWN, self.on_spin_down)

    vSizer = wx.BoxSizer(wx.VERTICAL)
    sizer = wx.BoxSizer(wx.HORIZONTAL)
    sizer.Add(self.text, 0, wx.CENTER)
    sizer.Add(self.spin, 0, wx.CENTER)
    vSizer.Add(sizer, 1, wx.CENTER)
    panel.SetSizer(vSizer)

def OnSpin(self, event):
    self.text.SetValue(str(event.GetPosition()))
   
def on_spin_up(self, event):
    print "up"

def on_spin_down(self, event):
    print "down"

Run the program

if name == “main”:
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()

Thank you, Bruce

···

On Sunday, April 13, 2014 11:23:34 PM UTC+3, bruce g wrote:

Hi Steve,

The below works.
I modified Mike Driscoll’s wx.SpinButton example to handle spin up and down events.
See: http://www.blog.pythonlibrary.org/2010/06/10/wxpython-a-tour-of-buttons-part-2-of-2/
Yes, I am aware there is a mixture of old and new binding methods.

Bruce

import wx

class MyForm(wx.Frame):

def __init__(self):
    wx.Frame.__init__(self, None, wx.ID_ANY, "Spin Button Tutorial")
    panel = wx.Panel(self, wx.ID_ANY)

    self.text = wx.TextCtrl(panel, value="1")
    self.spin = wx.SpinButton(panel, style=wx.SP_VERTICAL)
    self.spin.SetRange(1, 100)
    self.spin.SetValue(1)

    self.Bind(wx.EVT_SPIN, self.OnSpin, self.spin)
   
    self.spin.Bind(wx.EVT_SPIN_UP, self.on_spin_up)
    self.spin.Bind(wx.EVT_SPIN_DOWN, self.on_spin_down)

    vSizer = wx.BoxSizer(wx.VERTICAL)
    sizer = wx.BoxSizer(wx.HORIZONTAL)
    sizer.Add(self.text, 0, wx.CENTER)
    sizer.Add(self.spin, 0, wx.CENTER)
    vSizer.Add(sizer, 1, wx.CENTER)
    panel.SetSizer(vSizer)

def OnSpin(self, event):
    self.text.SetValue(str(event.GetPosition()))
   
def on_spin_up(self, event):
    print "up"

def on_spin_down(self, event):
    print "down"

Run the program

if name == “main”:
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()