2016-05-09 60 views
0

我有一個形式的多個部件,像這樣:wxSlider更新wxTextCtrl反之亦然

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) 

我有些事件勢必像這樣一個十幾滑塊/文本控件:

 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'])) 

然後我有這些功能:

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

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

它工作的很好,當我修改滑塊 - 它更新textctrl與適當的價值。但是,當我嘗試編輯textctrl時,它讓我選擇文本,但不實際編輯它。我試過wx.EVT_TEXT_ENTER以及沒有運氣。

如何使文本ctrl可更新並讓它更新滑塊的值?

回答

1

將textctrl上的樣式設置爲包含wx.TE_PROCESS_ENTER,然後您可以在綁定上使用EVT_TEXT_ENTER。

#!/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