0

I'm Building a gui using wxpython.. i have buttons and radiobuttons in it, and i wanna

to do such tasks based on what the current values of the widgets in the gui .. the insert

button has a multiple if statement one of these if statement is to check whether a

radiobutton is selected. i dont want to bind an event to the radio button so i have

checked about it in the insert button using

this is the defined radion button

self.rb1 = wx.RadioButton(self.panel, -1, 'Is this a required pre_action to the next   

step?', (5, 220))

and this is the check condition

if self.rb1.GetValue():

    # do something here

and also :

if self.rb1.GetValue() == 'True':

  # do some tasks

in both way (which is already the same) the is nothing happend when i choose the radio

button rb1 ! so what is the problwm of this ?

shaimaa
  • 37
  • 1
  • 3
  • 9
  • i just know this :), i accepted it as answer and a lot of thanks to him and to you :) – shaimaa Oct 07 '11 at 20:10
  • To check for True in python, you should use `if self.rb1.GetValue():` which evaulates to True or False ([ref](http://stackoverflow.com/a/2020704/2327328)) – philshem Jul 13 '16 at 20:42

1 Answers1

1

I don't know what that doesn't work for you. It works for me just fine. See sample code below:

import wx

class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")
        panel = wx.Panel(self, wx.ID_ANY)

        self.radio = wx.RadioButton(panel, label="Test", style = wx.RB_GROUP)
        self.radio2 = wx.RadioButton(panel, label="Test2")

        btn = wx.Button(panel, label="Check Radio")
        btn.Bind(wx.EVT_BUTTON, self.onBtn)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.radio, 0, wx.ALL, 5)
        sizer.Add(self.radio2, 0, wx.ALL, 5)
        sizer.Add(btn, 0, wx.ALL, 5)
        panel.SetSizer(sizer)

    #----------------------------------------------------------------------
    def onBtn(self, event):
        """"""
        print "First radioBtn = ", self.radio.GetValue()
        print "Second radioBtn = ", self.radio2.GetValue()

# Run the program
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyForm().Show()
    app.MainLoop()
Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88
  • yes it works fine when i bind an event to the radi button, but am asking not for using a binded event.. i dont want to use a bind method .. i wanna check the radio button value inside the the insert button code .. – shaimaa Oct 03 '11 at 15:27
  • I'm not bound to the radio button. I am bound to a regular wx.Button and checking the radio button's value in that handler. Isn't that what you asked for? – Mike Driscoll Oct 03 '11 at 18:03
  • ah! am sorry, i did not recognized that, am sorry .. yes that what am asking , and i previously did that and print the value of the radio buttons and i got "True" .. but i dont know why the statements after the condition " if self.rb1.GetValue(): " do not executed at all! – shaimaa Oct 03 '11 at 18:12
  • Oh. You need to make the conditional: if self.rb1.GetValue() == True. Otherwise you're comparing a bool and a string. – Mike Driscoll Oct 03 '11 at 18:19