Radio buttons are useful when you want the user to select one item out of a list of multiple items. Alternatively, the user can check a single radio button that cannot be unchecked. A group of radio buttons is started by setting the style wxRB_GROUP on the first button, and a lone radio button is created by setting wxRB_SINGLE as the style. Let's put together an application that uses radio buttons: from wxPython.wx import * class Window ( wxFrame ): def __init__ ( self ): wxFrame.__init__ ( self, None, -1, 'Ballot' ) # Create a panel, like we always do self.panel = wxPanel ( self, -1 ) # Create a grid layout self.sizer = wxGridBagSizer ( 2, 10 ) # Create one group of buttons ( plus a label ) and add them # We mark the beginning of the group with wxRB_GROUP self.president1 = wxRadioButton ( self.panel, -1, 'CharlesFagundes', style = wxRB_GROUP ) self.president2 = wxRadioButton ( self.panel, -1, 'TerriWells' ) self.sizer.Add ( wxStaticText ( self.panel, -1, 'President' ), ( 0, 0 ) ) self.sizer.Add ( self.president1, ( 1, 0 ) ) self.sizer.Add ( self.president2, ( 2, 0 ) ) # Create another group of radio buttons self.senator1 = wxRadioButton ( self.panel, -1, 'Peyton McCullough', style = wxRB_GROUP ) self.senator2 = wxRadioButton ( self.panel, -1, 'Alejandro Gervasio' ) self.senator3 = wxRadioButton ( self.panel, -1, 'Jagadish Chatarji' ) self.senator4 = wxRadioButton ( self.panel, -1, 'Michael Swanson' ) self.sizer.Add ( wxStaticText ( self.panel, -1, 'Senator' ), ( 0, 1 ) ) self.sizer.Add ( self.senator1, ( 1, 1 ) ) self.sizer.Add ( self.senator2, ( 2, 1 ) ) self.sizer.Add ( self.senator3, ( 3, 1 ) ) self.sizer.Add ( self.senator4, ( 4, 1 ) ) # Create yet another group of radio buttons # These will not be in a group # We'll also create a button, which will be activated if both boxes are checked self.term1 = wxRadioButton ( self.panel, 1, 'This is my only ballot.', style = wxRB_SINGLE ) self.term2 = wxRadioButton ( self.panel, 1, 'I swear it is!', style = wxRB_SINGLE ) self.button = wxButton ( self.panel, 2, 'Submit' ) self.button.Disable() self.sizer.Add ( wxStaticText ( self.panel, -1, 'Agreement' ), ( 0, 2 ) ) self.sizer.Add ( self.term1, ( 1, 2 ) ) self.sizer.Add ( self.term2, ( 2, 2 ) ) self.sizer.Add ( self.button, ( 3, 2 ), ( 2, 1 ), wxALIGN_CENTER ) # Hook up events to the three controls above EVT_RADIOBUTTON ( self.panel, 1, self.confirm ) EVT_BUTTON ( self.panel, 2, self.vote ) # Attach our sizer to our panel self.panel.SetSizerAndFit ( self.sizer ) # Resize the window self.sizer.Fit ( self ) self.Show ( True ) # This will check to see if term1 and term2 have both been clicked # If so, the button will be disabled def confirm ( self, event ): if ( self.term1.GetValue() == True ) & ( self.term2.GetValue() == True ): self.button.Enable() # Here, we will send a message to the user and reset everything def vote ( self, event ): # Show a dialog dialog = wxMessageDialog ( self, 'Thank you for your vote.', 'Thank You', style = wxOK ) dialog.ShowModal() dialog.Destroy() # Reset things self.president1.SetValue ( True ) self.senator1.SetValue ( True ) self.term1.SetValue ( False ) self.term2.SetValue ( False ) self.button.Disable() application = wxPySimpleApp() Window() application.MainLoop() Simple, huh?
blog comments powered by Disqus |
|
|
|
|
|
|
|