wxpythonwxtextctrl

wxSlider updating wxTextCtrl and Vice Versa


I have multiple widgets in a form like so:

    self.sl_qZone1 = wx.Slider(self, -1, 50, integer=True, 0, 100, size=(sliderWidthHeight), style=wx.SL_HORIZONTAL)
    self.tc_qZone1 = wx.TextCtrl(panel, -1, 50, (0,0), style=wx.TE_CENTER)

I have some events bound like so to a dozen or so sliders/text controls:

        wx.FindWindowByName(mvName['sl0']).Bind(wx.EVT_SLIDER, lambda event: self.sliderUpdate(event, wx.FindWindowByName(mvName['sl0']), wx.FindWindowByName(mvName['tc0']),), wx.FindWindowByName(mvName['sl0']))
        wx.FindWindowByName(mvName['tc0']).Bind(wx.EVT_CHAR, lambda event: self.tcVolUpdate(event, wx.FindWindowByName(mvName['sl0']), wx.FindWindowByName(mvName['tc0']),), wx.FindWindowByName(mvName['tc0']))

And then I have these functions:

    def sliderUpdate(self, event, slider, textctrl):
        textctrl.SetValue(str(slider.GetValue()))

    def tcVolUpdate(self, event, slider, textctrl):
        slider.SetValue(int(textctrl.GetValue()))    

It works great when I modify the slider -- it updates the textctrl with the appropriate value. But when I try to edit the textctrl, it lets me select the text but not actually edit it. I've tried wx.EVT_TEXT_ENTER as well with no luck.

How do I make the text ctrl updatable and have it update the value of the slider?


Solution

  • Set the style on the textctrl to include wx.TE_PROCESS_ENTER, then you can use EVT_TEXT_ENTER on the bind.

    #!/usr/bin/python  
    import wx
    
    class ex2(wx.Dialog):
        def __init__(self, parent, id, title):
            wx.Dialog.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(400, 420))
    
            panel = wx.Panel(self, -1)
            vbox = wx.BoxSizer(wx.HORIZONTAL)
            self.sl_qZone1 = wx.Slider(panel, -1, 50, 0, 100, size=(20,200), style=wx.SL_HORIZONTAL)
            self.tc_qZone1 = wx.TextCtrl(panel, -1, value="", style=wx.TE_CENTER|wx.TE_PROCESS_ENTER)
    
            self.sl_qZone1.Bind(wx.EVT_SLIDER, lambda event: self.sliderUpdate(event, self.sl_qZone1,self.tc_qZone1))
            self.tc_qZone1.Bind(wx.EVT_TEXT_ENTER, lambda event: self.tcVolUpdate(event, self.sl_qZone1,self.tc_qZone1))
    
            vbox.Add(self.sl_qZone1, 1, wx.EXPAND | wx.TOP | wx.RIGHT | wx.LEFT | wx.BOTTOM, 15)
            vbox.Add(self.tc_qZone1, 1, wx.ALIGN_CENTER | wx.TOP | wx.BOTTOM, 20)
            panel.SetSizer(vbox)
            self.Centre()
    
        def sliderUpdate(self, event, slider, textctrl):
            textctrl.SetValue(str(slider.GetValue()))
    
        def tcVolUpdate(self, event, slider, textctrl):
            slider.SetValue(int(textctrl.GetValue()))    
    
    
    class MyApp(wx.App):
        def OnInit(self):
            frame = ex2(None, -1, 'Example 2')
            frame.ShowModal()
            frame.Destroy()
            return True