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 ?

2
  • i just know this :), i accepted it as answer and a lot of thanks to him and to you :) Commented Oct 7, 2011 at 20:10
  • To check for True in python, you should use if self.rb1.GetValue(): which evaulates to True or False (ref) Commented Jul 13, 2016 at 20:42

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

4 Comments

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 ..
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?
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!
Oh. You need to make the conditional: if self.rb1.GetValue() == True. Otherwise you're comparing a bool and a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.