wxSlider更新wxTextCtrl和副Versa

时间:2016-05-09 01:22:10

标签: wxpython 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 个答案:

答案 0 :(得分: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