2014-01-20 36 views
0

我想使用wx.Choice組件來創建一個簡單的應用程序。如何將wx.Choice組件中的項目綁定到函數?

這個想法是,wx.Choice中列出了一些項目,每個項目都會觸發一個獨特的功能。

我的代碼,第一個版本是:

class MainFrame(wx.Frame): 

    def __init__(self): 
     wx.Frame.__init__(self, None, 0, 'wxPython pull-down choice', size = (400, 300)) 
     panel_select_model= wx.Panel(self, -1) 
     model_list = ['Test_A', 'Test_B'] 
     self._model_type = None 

     self._stat_tex = wx.StaticText(panel_select_model, 1, "Select Model Type:", (15, 20)) 
     self._droplist = wx.Choice(panel_select_model, 2, (150, 18), choices = model_list) 

     """ Bind A Panel """ 
     self._droplist.SetSelection(0) 
     self._droplist.Bind(wx.EVT_CHOICE, self.Test_A_click) 

但事實證明,在下拉列表中的兩個項目將觸發相同的功能(我期望Test_A將觸發功能和Test_B乾脆什麼也不做)。因此,我嘗試綁定Test_B另一種方法Test_B_click

class MainFrame(wx.Frame): 

    def __init__(self): 
     wx.Frame.__init__(self, None, 0, 'wxPython pull-down choice', size = (400, 300)) 
     panel_select_model= wx.Panel(self, -1) 
     model_list = ['Test_A', 'Test_B'] 
     self._model_type = None 

     self._stat_tex = wx.StaticText(panel_select_model, 1, "Select Model Type:", (15, 20)) 
     self._droplist = wx.Choice(panel_select_model, 2, (150, 18), choices = model_list) 

     """ Bind A Panel """ 
     self._droplist.SetSelection(0) 
     self._droplist.Bind(wx.EVT_CHOICE, self.Test_A_click) 

     """ Bind B Panel """ 
     self._droplist.SetSelection(1) 
     self._droplist.Bind(wx.EVT_CHOICE, self.Test_B_click) 

上面的代碼顯示的主框架,在它的下降列表,並在下拉列表中的兩個項目。但是,當我單擊這兩個項目中的任何一個時,都不再觸發任何功能。

那麼,我該如何實現我的目標:將不同的函數綁定到wx.Choice組件中的每個項目,並使它們正確觸發函數。

感謝。

回答

0

我回來問自己的問題。我不認爲這是完美的解決方案,但它解決了問題。

下面是代碼:

級的主機(wx.Frame):

def __init__(self): 
    wx.Frame.__init__(self, None, 0, 'wxPython pull-down choice', size = (400, 300)) 
    panel_select_model= wx.Panel(self, -1) 
    model_list = ['Test_A', 'Test_B'] 
    self._model_type = None 

    self._stat_tex = wx.StaticText(panel_select_model, 1, "Select Model Type:", (15, 20)) 
    self._droplist = wx.Choice(panel_select_model, 2, (150, 18), choices = model_list) 

    """ Bind A Panel """ 
    self._droplist.SetSelection(0) 
    self._droplist.Bind(wx.EVT_CHOICE, self.choice_click) 

    """ Bind B Panel """ 
    self._droplist.SetSelection(1) 
    self._droplist.Bind(wx.EVT_CHOICE, self.choice_click) 

def choice_click(self, event): 
    if self._droplist.GetStringSelection() == "Test_A": 
     self.Test_A__click() 
    elif self._droplist.GetStringSelection() == "Test_B": 
     self.Test_B_click() 

在上面的代碼中。我在wx.Choice組件和我想要觸發的函數之間添加了另一個層。一旦匹配,其相應的功能將被觸發。

恐怕這不是一個有效的方法。所以如果有人能提出一個好的解決方案,我將非常感激。非常感謝。

2

你的方法可能是最常用的方法來做你想做的事情,並且很容易知道發生了什麼。但是,如果你這樣做與元編程的一個小破折號,如果你正確設置它,可能是有點清潔:

import wx 

######################################################################## 
class MyFrame(wx.Frame): 

    #---------------------------------------------------------------------- 
    def __init__(self): 
     """Constructor""" 
     wx.Frame.__init__(self, None, title="Callbacks") 
     panel = wx.Panel(self) 
     self.choice = wx.Choice(panel, choices=["test_A", "test_B"]) 
     self.choice.Bind(wx.EVT_CHOICE, self.onChoice) 
     self.Show() 

    #---------------------------------------------------------------------- 
    def onChoice(self, event): 
     choice = self.choice.GetStringSelection() 
     method = getattr(self, "on_%s" % choice) 
     method() 

    #---------------------------------------------------------------------- 
    def on_test_A(self): 
     print "You chose test_A!" 

    #---------------------------------------------------------------------- 
    def on_test_B(self): 
     print "You chose test_B!" 


#---------------------------------------------------------------------- 
if __name__ == "__main__": 
    app = wx.App(False) 
    frame = MyFrame() 
    app.MainLoop() 

在這裏我們使用Python的GETATTR功能,以確定哪些方法來實際調用。這使我們可以跳過使用如果聲明,但假定我們已經爲控件中的所有選項設置了所有方法。

相關問題