wx.RadioButton questions

Hi. A few odd questions about wx.RadioButton()

1. I'm trying to change the text of the wx.RadioButton() while my program is running. I've tried SetLabel() and have had no good results. What should I do?

2. I'm also trying to add additional buttons to the Radio Group AFTER the program is running, and resize the entire layout. But once again, that's not working. The added buttons all get clumped in the upper-left hand corner of the window.

Any suggestions?

Simson

Hi Simson,

Hi. A few odd questions about wx.RadioButton()

1. I'm trying to change the text of the wx.RadioButton() while my program is running. I've tried SetLabel() and have had no good results. What should I do?

Not sure what you're doing since you didn't post any code. When I use SetLabel, it works (see code below).

2. I'm also trying to add additional buttons to the Radio Group AFTER the program is running, and resize the entire layout. But once again, that's not working. The added buttons all get clumped in the upper-left hand corner of the window.

This is a wxPython "got'cha".The stacked widget appearance that you saw is a hint. If you resize your frame, it'll probably "pop" into place. If so, then you'll just need to add a Layout() call after adding new widgets. I've whipped up a little sample for you...

<code>

import wx
class MyForm(wx.Frame):
     def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "RadioButton Tutorial")
         # Add a panel so it looks the correct on all platforms
        panel = wx.Panel(self, wx.ID_ANY)
        self.panel = panel
        self.radio_one = wx.RadioButton(panel, wx.ID_ANY, "Radio 1", style=wx.RB_GROUP)
        self.radio_two = wx.RadioButton(panel, wx.ID_ANY, "Radio 2")
## print dir(radio_one)
        btn = wx.Button(panel, wx.ID_ANY, 'Push me!')
        self.Bind(wx.EVT_BUTTON, self.onButton, btn)

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

    def onButton(self, event):
        self.radio_one.SetLabel("bork")

        newRadio = wx.RadioButton(self.panel, wx.ID_ANY, "New Radio button!")
        self.sizer.Add(newRadio)
        self.panel.Layout()
         # Run the program
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyForm().Show()
    app.MainLoop()

</code>

The above is known as a "small runnable example" or a "sample application". Normally the user asking the question sends one of these to demonstrate their issue. See the wiki for more info: http://wiki.wxpython.org/MakingSampleApps

My example above should "just work". I used wxPython 2.8.9.1 (unicode) on Windows XP with Python 2.5.

Any suggestions?

Simson

Have fun!

···

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org

Mike

1. Thanks for posting this. I didn't post code because, well, I forgot. Thanks for posting the example.

I'm a bit new to wxpython.

2. My error is that I was calling Fit() and not Layout(). Duh.

3. I remain confused by the inconsistent use of accessor methods for properties.

Some properties are set and get with SetProperty() and GetProperty() while others are set with just Property() while still others are get with just Property().

For example, wx.Control uses SetLabel() to set the label and GetLabelText() to get it.

What's the difference between a Label and a LabelText?

But that same control uses GetAlignment() to get the alignment and doesn't seem to have any means to set the Alignment after-the-fact. (or am I missing it?)

4. Where is the right place to find out about all the methods? On the web page:
  http://www.wxpython.org/docs/api/wx.Control-class.html

There is mention of GetLabelText() but no mention of SetLabel().

It turns out that SetLabel() is described on the wx.Window web page. Which seems really weird, since wx.Window objects don't show labels.

Thanks!

Hi Simson,

Mike

1. Thanks for posting this. I didn't post code because, well, I forgot. Thanks for posting the example.

No problem.

I'm a bit new to wxpython.

Welcome to the world of wxPython! It's fun, if a bit confusing at first...

2. My error is that I was calling Fit() and not Layout(). Duh.

Layout() basically forces the sizers to recalculate the widgets placements and such and I think it may call Refresh when it's done, although I am not sure. As I understand it, Fit() usually justs "fits" the widgets to the smallest area it can inside of the parent (a wx.Frame or wx.Panel).

3. I remain confused by the inconsistent use of accessor methods for properties.

Some properties are set and get with SetProperty() and GetProperty() while others are set with just Property() while still others are get with just Property().

For example, wx.Control uses SetLabel() to set the label and GetLabelText() to get it.

What's the difference between a Label and a LabelText?

But that same control uses GetAlignment() to get the alignment and doesn't seem to have any means to set the Alignment after-the-fact. (or am I missing it?)

This gets confusing because there is a lot of inheritance hoodoo going on here. Most of the widgets inherit properties from another class, so the docs usually only show the new methods of the widget and not the methods from that which it inherits from, unless said widget is overriding the inherited method. I usually find that it's faster to just use Python's builtin introspection tools to find out the methods of my widgets. Do something like this:

myWidget = wx.SomeWidget()
print dir(myWidget)

or

import wx
print dir(wx.StaticText)

In the latter case, you would get something like this:

['AcceleratorTable', 'AcceptsFocus', 'AcceptsFocusFromKeyboard', 'AddChild', 'AddPendingEvent', 'AdjustForLayoutDirection', 'Alignment', 'AssociateHandle', 'AutoLayout', 'BackgroundColour', 'BackgroundStyle', 'BestSize', 'BestVirtualSize', 'Bind', 'Border', 'CacheBestSize', 'CanSetTransparent', 'CaptureMouse', 'Caret', 'Center', 'CenterOnParent', 'Centre', 'CentreOnParent', 'CharHeight', 'CharWidth', 'Children', 'ClassName', 'ClearBackground', 'ClientAreaOrigin', 'ClientRect', 'ClientSize', 'ClientToScreen', 'ClientToScreenXY', 'ClientToWindowSize', 'Close', 'Command', 'Connect', 'Constraints', 'ContainingSizer', 'ConvertDialogPointToPixels', 'ConvertDialogSizeToPixels', 'ConvertPixelPointToDialog', 'ConvertPixelSizeToDialog', 'Create', 'Cursor', 'DLG_PNT', 'DLG_SZE', 'DefaultAttributes', 'Destroy', 'DestroyChildren', 'Disable', 'Disconnect', 'DissociateHandle', 'DragAcceptFiles', 'DropTarget', 'EffectiveMinSize', 'Enable', 'Enabled', 'EventHandler', 'EvtHandlerEnabled', 'ExtraStyle', 'FindFocus', 'FindWindowById', 'FindWindowByLabel', 'FindWindowByName', 'Fit', 'FitInside', 'Font', 'ForegroundColour', 'Freeze', 'GetAcceleratorTable', 'GetAdjustedBestSize', 'GetAlignment', 'GetAutoLayout', 'GetBackgroundColour', 'GetBackgroundStyle', 'GetBestFittingSize', 'GetBestSize', 'GetBestSizeTuple', 'GetBestVirtualSize', 'GetBorder', 'GetCapture', 'GetCaret', 'GetCharHeight', 'GetCharWidth', 'GetChildren', 'GetClassDefaultAttributes', 'GetClassName', 'GetClientAreaOrigin', 'GetClientRect', 'GetClientSize', 'GetClientSizeTuple', 'GetConstraints', 'GetContainingSizer', 'GetCursor', 'GetDefaultAttributes', 'GetDropTarget', 'GetEffectiveMinSize', 'GetEventHandler', 'GetEvtHandlerEnabled', 'GetExtraStyle', 'GetFont', 'GetForegroundColour', 'GetFullTextExtent', 'GetGrandParent', 'GetGtkWidget', 'GetHandle', 'GetHelpText', 'GetHelpTextAtPoint', 'GetId', 'GetLabel', 'GetLabelText', 'GetLayoutDirection', 'GetMaxHeight', 'GetMaxSize', 'GetMaxWidth', 'GetMinHeight', 'GetMinSize', 'GetMinWidth', 'GetName', 'GetNextHandler', 'GetParent', 'GetPosition', 'GetPositionTuple', 'GetPreviousHandler', 'GetRect', 'GetScreenPosition', 'GetScreenPositionTuple', 'GetScreenRect', 'GetScrollPos', 'GetScrollRange', 'GetScrollThumb', 'GetSize', 'GetSizeTuple', 'GetSizer', 'GetTextExtent', 'GetThemeEnabled', 'GetToolTip', 'GetTopLevelParent', 'GetUpdateClientRect', 'GetUpdateRegion', 'GetValidator', 'GetVirtualSize', 'GetVirtualSizeTuple', 'GetWindowBorderSize', 'GetWindowStyle', 'GetWindowStyleFlag', 'GetWindowVariant', 'GrandParent', 'GtkWidget', 'Handle', 'HasCapture', 'HasFlag', 'HasMultiplePages', 'HasScrollbar', 'HasTransparentBackground', 'HelpText', 'Hide', 'HitTest', 'HitTestXY', 'Id', 'InheritAttributes', 'InheritsBackgroundColour', 'InitDialog', 'InvalidateBestSize', 'IsBeingDeleted', 'IsDoubleBuffered', 'IsEnabled', 'IsExposed', 'IsExposedPoint', 'IsExposedRect', 'IsFrozen', 'IsRetained', 'IsSameAs', 'IsShown', 'IsShownOnScreen', 'IsTopLevel', 'Label', 'LabelText', 'Layout', 'LayoutDirection', 'LineDown', 'LineUp', 'Lower', 'MakeModal', 'MaxHeight', 'MaxSize', 'MaxWidth', 'MinHeight', 'MinSize', 'MinWidth', 'Move', 'MoveAfterInTabOrder', 'MoveBeforeInTabOrder', 'MoveXY', 'Name', 'Navigate', 'NewControlId', 'NextControlId', 'NextHandler', 'OnPaint', 'PageDown', 'PageUp', 'Parent', 'PopEventHandler', 'PopupMenu', 'PopupMenuXY', 'Position', 'PostCreate', 'PrepareDC', 'PrevControlId', 'PreviousHandler', 'ProcessEvent', 'ProcessPendingEvents', 'PushEventHandler', 'Raise', 'Rect', 'Refresh', 'RefreshRect', 'RegisterHotKey', 'ReleaseMouse', 'RemoveChild', 'RemoveEventHandler', 'Reparent', 'ScreenPosition', 'ScreenRect', 'ScreenToClient', 'ScreenToClientXY', 'ScrollLines', 'ScrollPages', 'ScrollWindow', 'SendSizeEvent', 'SetAcceleratorTable', 'SetAutoLayout', 'SetBackgroundColour', 'SetBackgroundStyle', 'SetBestFittingSize', 'SetCaret', 'SetClientRect', 'SetClientSize', 'SetClientSizeWH', 'SetConstraints', 'SetContainingSizer', 'SetCursor', 'SetDimensions', 'SetDoubleBuffered', 'SetDropTarget', 'SetEventHandler', 'SetEvtHandlerEnabled', 'SetExtraStyle', 'SetFocus', 'SetFocusFromKbd', 'SetFont', 'SetForegroundColour', 'SetHelpText', 'SetHelpTextForId', 'SetId', 'SetInitialSize', 'SetLabel', 'SetLayoutDirection', 'SetMaxSize', 'SetMinSize', 'SetName', 'SetNextHandler', 'SetOwnBackgroundColour', 'SetOwnFont', 'SetOwnForegroundColour', 'SetPosition', 'SetPreviousHandler', 'SetRect', 'SetScrollPos', 'SetScrollbar', 'SetSize', 'SetSizeHints', 'SetSizeHintsSz', 'SetSizeWH', 'SetSizer', 'SetSizerAndFit', 'SetThemeEnabled', 'SetToolTip', 'SetToolTipString', 'SetTransparent', 'SetValidator', 'SetVirtualSize', 'SetVirtualSizeHints', 'SetVirtualSizeHintsSz', 'SetVirtualSizeWH', 'SetWindowStyle', 'SetWindowStyleFlag', 'SetWindowVariant', 'ShouldInheritColours', 'Show', 'Shown', 'Size', 'Sizer', 'Thaw', 'ThemeEnabled', 'ToggleWindowStyle', 'ToolTip', 'TopLevel', 'TopLevelParent', 'TransferDataFromWindow', 'TransferDataToWindow', 'Unbind', 'UnregisterHotKey', 'Update', 'UpdateClientRect', 'UpdateRegion', 'UpdateWindowUI', 'UseBgCol', 'Validate', 'Validator', 'VirtualSize', 'WarpPointer', 'WindowStyle', 'WindowStyleFlag', 'WindowToClientSize', 'WindowVariant', 'Wrap', '__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__', '_setOORInfo', 'thisown']

Yes, that simple widget is, in fact, quite complicated. I personally recommend getting Robin Dunn's "wxPython in Action" book. It talks about most of the common widgets and lists their methods.

4. Where is the right place to find out about all the methods? On the web page:
    wxPython API Documentation — wxPython Phoenix 4.2.2 documentation

This is the official docs (man pages) that are generated from source (I think). There's also this set which has been tweaked some by Andrea Gavana: http://xoomer.alice.it/infinity77/wxPython/

It sounds like there will be another set of docs to be released sooner or later...

There is mention of GetLabelText() but no mention of SetLabel().

It turns out that SetLabel() is described on the wx.Window web page. Which seems really weird, since wx.Window objects don't show labels.

Thanks!

Not sure why that's there...but I think the control inherits that method from wx.Window.

Mike

Simson Garfinkel wrote:

3. I remain confused by the inconsistent use of accessor methods for properties.

Some properties are set and get with SetProperty() and GetProperty() while others are set with just Property() while still others are get with just Property().

For example, wx.Control uses SetLabel() to set the label and GetLabelText() to get it.

What's the difference between a Label and a LabelText?

$ pydoc wx.Control.GetLabel
Help on method GetLabel in wx.Control:

wx.Control.GetLabel = GetLabel(*args, **kwargs) unbound wx._core.Control method
     GetLabel(self) -> String

     Generic way of getting a label from any window, for identification
     purposes. The interpretation of this function differs from class to
     class. For frames and dialogs, the value returned is the title. For
     buttons or static text controls, it is the button text. This function
     can be useful for meta-programs such as testing tools or special-needs
     access programs which need to identify windows by name.

$ pydoc wx.Control.GetLabelText
Help on method GetLabelText in wx.Control:

wx.Control.GetLabelText = GetLabelText(*args, **kwargs) unbound wx._core.Control method
     GetLabelText(self) -> String

     Get just the text of the label, without mnemonic characters ('&')

But that same control uses GetAlignment() to get the alignment and doesn't seem to have any means to set the Alignment after-the-fact. (or am I missing it?)

Alignment (for those widgets that support it) is done by setting the window style flag. GetAlignment is a convenience function for extracting just the alignment flags from the style.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Mike,

Thanks for the response.

1. Layout() did the trick.

2. Yes, the Label Text for the wx.RadioButton is inherited from the wx.Window control. Which seems wrong, since Windows don't have labels.

3. My main complaint about the accessor methods is that they are all so inconsistent in their use or absence of the Get() modifier, and calling the labels Label in some cases and LabelText in other cases.

4. Printing a dir() is a trick that I've shown many of my students. Another one is running help() on an object. I wish we had a decent graphical inspector, though. Do you know if one exists?

Thanks again.

Simson Garfinkel wrote:

Mike,

Thanks for the response.

1. Layout() did the trick.

Cool!

2. Yes, the Label Text for the wx.RadioButton is inherited from the wx.Window control. Which seems wrong, since Windows don't have labels.

3. My main complaint about the accessor methods is that they are all so inconsistent in their use or absence of the Get() modifier, and calling the labels Label in some cases and LabelText in other cases.

Hopefully you read Robin's answer, which I think helped in understanding your issues in points 2 and 3.

4. Printing a dir() is a trick that I've shown many of my students. Another one is running help() on an object. I wish we had a decent graphical inspector, though. Do you know if one exists?

Thanks again.

There is the Widget Inspection Tool: http://wiki.wxpython.org/Widget%20Inspection%20Tool

I'm not sure if that's what you want though. I like the source assistant that's in WingWare's IDE, although it doesn't always pick up everything in wxPython due to its "sandwiching" of C++ and Python code.

Mike