I can't figure out how to set a radio button within a filemenu. I can use myRadioButton.SetValue( True ) outside of a filemenu.

What I'm trying to do is to automatically set a particular radio button dependent upon the last value used.

import wx

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)

        # Menu Bar
        self.frame_1_menubar = wx.MenuBar()
        self.name = wx.Menu()
        self.one = wx.MenuItem(self.name, 101, "one", "", wx.ITEM_RADIO)
        self.name.AppendItem(self.one)
        self.two = wx.MenuItem(self.name, 102, "two", "", wx.ITEM_RADIO)
        self.name.AppendItem(self.two)
        self.three = wx.MenuItem(self.name, 103, "three", "", wx.ITEM_RADIO)
        self.name.AppendItem(self.three)
        self.frame_1_menubar.Append(self.name, "time out")
        self.SetMenuBar(self.frame_1_menubar)
        # Menu Bar end

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_MENU, self.on_one, self.one)
        self.Bind(wx.EVT_MENU, self.on_two, self.two)
        self.Bind(wx.EVT_MENU, self.on_three, self.three)
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: MyFrame.__set_properties
        self.SetTitle("frame_1")
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MyFrame.__do_layout
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        self.Layout()
        # end wxGlade

    def on_three(self, event): # wxGlade: MyFrame.<event_handler>
        print "Event handler `on_three' not implemented!"
        event.Skip()

    def on_two(self, event): # wxGlade: MyFrame.<event_handler>
        print "Event handler `on_two' not implemented!"
        event.Skip()

    def on_one( self, event): # wxGlade: MyFrame.<event_handler>
        print "Event handler `on_four' not implemented!"
        event.Skip()

# end of class MyFrame

if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    frame_1 = MyFrame(None, -1, "")
    app.SetTopWindow(frame_1)
    frame_1.Show()
    app.MainLoop()

Recommended Answers

All 2 Replies

Here is one example how to do this ...

# a look at wxPython menu radio buttons

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, mytitle, mysize):
        wx.Frame.__init__(self, parent, -1, mytitle, size=mysize)
        #self.SetBackgroundColour("white")

        # set up the menu
        file = wx.Menu()
        color = wx.Menu()
        self.color = color
        help = wx.Menu()
        
        id_blue = 1001
        id_yellow = 1002
        id_red = 1003
        # append the background color drop down items in order
        # uses radiobuttons, item has to be a wxPython color
        c_blue = color.AppendRadioItem(id_blue, "blue")
        c_yellow = color.AppendRadioItem(id_yellow, "yellow")
        c_red = color.AppendRadioItem(id_red, "red")            

        # create the menubar near the top of the frame
        menuBar = wx.MenuBar()
        # adding the menus to the MenuBar
        menuBar.Append(file,"&File")
        menuBar.Append(color,"&Color")
        menuBar.Append(help,"&Help")
        # now add the MenuBar to the frame
        self.SetMenuBar(menuBar)
        
        # set radio button c_red to checked and change color
        # (default would be first button c_blue is checked)
        self.color.Check(id_red, True)
        self.onColor()

        # bind the various menu events to an action
        self.Bind(wx.EVT_MENU, self.onColor, c_blue)
        self.Bind(wx.EVT_MENU, self.onColor, c_yellow)
        self.Bind(wx.EVT_MENU, self.onColor, c_red)
        
    def onColor(self, event=None):
        """change the editor background color"""
        for item in self.color.GetMenuItems():
            if item.IsChecked():
                # set new color
                self.SetBackgroundColour(item.GetLabel())
                # needed to update color
                self.Refresh()


app = wx.App(0)
# create a MyFrame instance and show the frame
mytitle = 'Only Color Menu is active!'
width = 400
height = 300
MyFrame(None, mytitle, (width, height)).Show()
app.MainLoop()

Perfect! That's exactly what I was looking for. I should have asked a few hours earlier but I wanted to solve it on my own.

Thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.