如何在wx.选择组件到功能?

2024-04-27 22:28:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用wx.选择组件来归档一个简单的应用程序。在

我们的想法是,在wx.选择,每个函数都将触发一个唯一的函数。在

我的第一个版本是:

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

^{pr2}$

上面的代码显示一个主框架,其中有一个下拉列表和两个下拉列表项。但是,当我单击这两个项目中的任何一个时,不再触发任何功能。在

那么,如何实现我的目标:将不同的函数绑定到wx.选择组件,并使它们正确触发功能。

谢谢。在


Tags: 函数testselfnone列表modelinit组件
2条回答

我回来问我自己的问题。我不认为这是一个完美的解决办法,但它成功地解决了我的问题。在

代码如下:

类主机(wx.框架)公司名称:

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.选择组件和我想触发的函数。一旦找到匹配项,它相应的函数就会被触发。在

恐怕这不是一个有效的方法。如果有人能提出一个好的解决方案,我将不胜感激。谢谢。在

你的方法可能是做你想做的事情最常见的方法,而且很容易分辨出发生了什么。但是,如果您使用少量的元编程来执行此操作,如果设置正确,可能会更干净一些:

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函数来确定实际调用哪个方法。这允许我们跳过if语句的使用,但假设我们为控件中的所有选项设置了所有方法。在

相关问题 更多 >