连续检查单选按钮状态[WxPython]

时间:2014-06-19 17:44:06

标签: python wxpython

我有一组带有一组字符串的列表框。我想要显示的字符串集取决于选择了哪个单选按钮。我希望当用户与表单进行交互时,如果他们更改了单选按钮,它将更新列表框。

这是我的代码(我离开the array for t87 and t89因为它们很长(假设它们存在):

 def OnBtnSuperTesting(self, event):
    class MainWindow(wx.Frame):

        def __init__(self, parent, title):

            self.dirname=''

            wx.Frame.__init__(self, parent, title=title, size=(320,440))
            self.SetBackgroundColour(wx.WHITE)
            self.CenterOnScreen()
            self.CreateStatusBar()
            self.radioT89 = wx.RadioButton(self, -1, 'T89 only', pos = (2,0), style = wx.RB_GROUP)
            self.radioT87 = wx.RadioButton(self, -1, 'T87 only', pos = (154, 0))
            self.radioKeySort = wx.RadioButton(self, -1, 'Sort by Key', pos = (2,40), style = wx.RB_GROUP)
            self.radioAtoZ = wx.RadioButton(self, -1, 'Sort Name A-Z', pos = (2,60))
            self.radioZtoA = wx.RadioButton(self, -1, 'Sort Name Z-A', pos = (2,80))
            self.checkCode = wx.CheckBox(self, -1, 'Generate Code', pos = (154,40))
            self.checkBuild = wx.CheckBox(self, -1, 'Generate Build Report', pos = (154, 60))
            self.ln = wx.StaticLine(self, -1, pos = (0,15), size = (300,3), style = wx.LI_HORIZONTAL)
            self.ln2 = wx.StaticLine(self, -1, pos = (150,15), size = (3,100), style = wx.LI_VERTICAL)

            self.radioT87.Bind(wx.EVT_RADIOBUTTON, self.updateList)
            #self.Bind(wx.EVT_RADIOBUTTON, self.radioT89, self.updateList())




            self.listbox = wx.ListBox(self, -1, pos = (0,120), size = (300,200), choices = T89, style = (wx.LB_SINGLE|wx.LB_HSCROLL))
            self.go = wx.Button(self,-1, label = 'Go!', pos = (110, 325))



            # Setting up the menu.
            filemenu= wx.Menu()
            menuAbout= filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program")
            menuExit = filemenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program")

            # Creating the menubar.
            menuBar = wx.MenuBar()
            menuBar.Append(filemenu,"&File") 
            self.SetMenuBar(menuBar) 

            # Events.
            self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
            self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)

            self.SetAutoLayout(1)

            self.Show()



        def OnExit(self,e):
            self.Close(True)  # Close the frame.

        def updateList(self):
            if  self.radioT87.GetValue() == True:
                choices = T87
                self.listbox.Set(choices)
            else:
                choices = T89
                self.listbox.Set(choices)
    app = wx.App(False)
    frame = MainWindow(None, "Supervisory Testing")
    app.MainLoop()

2 个答案:

答案 0 :(得分:0)

创建每个单选按钮时,您可以创建绑定事件。这样做(正如您在代码中稍后实现的那样)是在发生绑定事件时执行命令功能。在你的情况下,它看起来像这样:

self.Bind(wx.EVT_RADIOBUTTON,self.RadioButton,self.DoSomething)

说明:

wx.EVT_RADIOBUTTON

这是用户更改Radiobutton状态时触发的事件。它可能有也可能没有属性。

self.RadioButton

这是您要绑定的单选按钮。在你的情况下" self.radioAtoZ"或类似的。

self.DoSomething

这是回调函数。您可以随心所欲地制作它,例如:

def DoSomething(self):
    if self.radioAtoZ.getStatus():
        rearrangeNumbersFromAtoZ
        print 'Re-arranged numbers from A to Z'
    else:
        etc.

编辑:

self.RadioButton.Bind(EVT_RADIOBUTTON, self.DoSomething)

你的自我结构.DoSomething应该是这样的:

Class MainWindow:
     def __init_(self, parent):


     def DoSomething(self):
         #dostuff

还回应您的其他评论: 当在Bind事件中调用函数时,它默认将事件传递给函数。此外,所有功能都有" self" arg,因此给出了args。您需要更改以下内容:

def DoSomething(self, event):
      #dostuff

答案 1 :(得分:0)

我决定重写OP的代码来演示如何将2个RadioButton绑定到2个不同的事件处理程序并更新ListBox:

import wx

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

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Radios!")
        panel = wx.Panel(self)

        sizer = wx.BoxSizer(wx.VERTICAL)

        self.radioAtoZ = wx.RadioButton(panel, label='Sort Name A-Z',
                                        style = wx.RB_GROUP)
        self.radioAtoZ.Bind(wx.EVT_RADIOBUTTON, self.sortAZ)
        sizer.Add(self.radioAtoZ, 0, wx.ALL|wx.EXPAND, 5)

        self.radioZtoA = wx.RadioButton(panel, label='Sort Name Z-A')
        self.radioZtoA.Bind(wx.EVT_RADIOBUTTON, self.sortZA)
        sizer.Add(self.radioZtoA, 0, wx.ALL|wx.EXPAND, 5)

        choices = ["aardvark", "zebra", "bat", "giraffe"]
        self.listbox = wx.ListBox(panel, choices=choices)
        sizer.Add(self.listbox, 0, wx.ALL, 5)

        panel.SetSizer(sizer)

        self.Show()

    #----------------------------------------------------------------------
    def sortAZ(self, event):
        """"""
        choices = self.listbox.GetStrings()
        choices.sort()
        self.listbox.SetItems(choices)

    #----------------------------------------------------------------------
    def sortZA(self, event):
        """"""
        choices = self.listbox.GetStrings()
        choices.sort()
        choices.reverse()
        self.listbox.SetItems(choices)

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

您将需要查看以下wiki文章,了解这种方式与其他方式的绑定差异:

大多数情况下,当您创建一组执行不同操作的小部件时,会将它们绑定到不同的事件处理程序。如果要将所有RadioButtons绑定到同一个处理程序,那么您可能需要使用唯一名称命名每个窗口小部件,以便当它们到达处理程序时,您可以确定按下了哪个按钮。然后,您可以使用一系列 if 语句来决定如何对列表框执行操作。这是一个讲述这类事情的教程:

相关问题