0

as in the title, is it possible to add a custom event listener for wx widget and if so, how? More particularly I am trying to add one which listens to the changes of SelStart and SelEnd attributes of wx.Slider. Thanks!

4
  • Do any of the events listed here docs.wxwidgets.org/trunk/classwx_slider.html work for your purposes? Commented Jun 14, 2016 at 17:39
  • There may be a complicated method to make it work with some of them, but I am also interested whether custom listeners are possible in general. Commented Jun 14, 2016 at 17:47
  • According to MSDN doc regarding TrackBar Control (on which wx.Slider is based), the range can be changed from code but there is no interaction from the user side. It also says that if one should want to limit valid slider positions to the range, this had to be implemented by the programmer. Is it that what you want? Commented Jun 15, 2016 at 15:43
  • yep. in my application the users clicks the left mouse button to set the selection start and right mouse button to set its end. I cannot use mouse click events for certain reasons, so wanted to know if a custom listener for the changes in these values is implementable. Commented Jun 15, 2016 at 16:31

1 Answer 1

2

Below is the code for custom listener for wx.slider, I am calling it inside other listener's handler.

You can call this customize listener from the point where SelStart and SelEnd changing.

import wx 
import wx.lib.newevent
DialogRespondEvent, EVT_DIALOG_RESPOND_EVENT = wx.lib.newevent.NewEvent()
class Mywin(wx.Frame): 
    
    def __init__(self, parent, title): 
        super(Mywin, self).__init__(parent, title = title,size = (250,150))  
        self.InitUI() 
 
    def InitUI(self):    
        pnl = wx.Panel(self) 
        vbox = wx.BoxSizer(wx.VERTICAL) 
        self.Bind(EVT_DIALOG_RESPOND_EVENT, self.testing) 
        self.sld = wx.Slider(pnl, value = 10, minValue = 1, maxValue = 100,
        style = wx.SL_HORIZONTAL|wx.SL_LABELS) 
        
        vbox.Add(self.sld,1,flag = wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL |     wx.TOP, border = 20) 
        self.sld.Bind(wx.EVT_SLIDER, self.OnSliderScroll) 
        self.txt = wx.StaticText(pnl, label = 'Hello',style = wx.ALIGN_CENTER)                
        vbox.Add(self.txt,1,wx.ALIGN_CENTRE_HORIZONTAL) 

        pnl.SetSizer(vbox) 
        self.Centre() 
        self.Show(True)

    def testing(self,evt):
        print "you can do whatever you want here"

   def OnSliderScroll(self, e): 
       obj = e.GetEventObject() 
       val = obj.GetValue() 
       font = self.GetFont() 
       font.SetPointSize(self.sld.GetValue()) 
       self.txt.SetFont(font)
       evt = DialogRespondEvent()
       wx.PostEvent(self, evt)

ex = wx.App() 
Mywin(None,'Slider demo') 
ex.MainLoop()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.