wxpython小部件的自定义事件侦听器

时间:2016-06-14 17:28:53

标签: python python-2.7 wxpython

如标题所示,是否可以为wx小部件添加自定义事件监听器,如果是,如何?更具体地说,我正在尝试添加一个侦听wx.Slider的SelStart和SelEnd属性的更改。 谢谢!

1 个答案:

答案 0 :(得分:2)

下面是wx.slider的自定义侦听器的代码,我在其他侦听器的处理程序中调用它。

  

您可以从SelStart和SelEnd更改的位置调用此自定义侦听器。

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()