wxPython Menu - deselect all Radio Buttons (wx.RADIO_ITEM)

1k views Asked by At

im working on my first wxpython app. Its a GUI for a comandline utility and needs the connection to a samba share. Now i want to add a server selection to the menu as radio buttons. Im using ConfigParser for a settings ini file which also holds the configuration for the default server as well as the server it should connect.

Now i want to deselect all radio buttons if no default server is selected and the program didnt connect at startup.

Here is my Code for adding the RADIO_ITEMS to the menu: (I know this could probably be done much better but im still learning and im glad it works, any recommendatiosn are welcome)

# Create a List to be used as variables
for i in range(1,7):
    list.append('radiobutton{}'.format(str(i)))
# Append the RadioButtons
for i in range(1,7):
    num = str(i)
    # Load Sections of ini (every server has its own section, up to 6 are allowed)
    config_sec = Config.sections()
    if filter(lambda x: 'Server{}'.format(num) in x, config_sec):
        name = LoadConfig("Server{}".format(num))['connection name']
        list[i] = wx.MenuItem(self.wpkg_server, 400+i, name, 'Connect to Server: {} ?'.format(name), kind=wx.ITEM_RADIO)
        self.wpkg_server.AppendItem(list[i])
        # Get Default server setting from ini
        default_server = LoadConfig("Options")['default server']
        # Try if default_server is a valid number that can be converted to int
        try:
            int(default_server)
        except ValueError:
            # This is the part not working, if there is no default server set i want to uncheck
            # deselect all added items but with no luck.
            # the first item is always selected if no default server was set
            list[i].Check(False)
        else:
            if i == int(default_server):
                # Selecting the correct radio button if it is the default server works fine
                list[i].Check(True)
                self.statusbar.SetStatusText('Connected to Server: {}!'.format(name))
2

There are 2 answers

2
Joran Beasley On
[menu.Check(False) for menu in my_list]

don't use list as your variable name, also I would expect you to get an IndexError with this code ....

0
Rolf of Saxony On

You are right Sonic, it appears that you must have something checked in menu RadioItems, so adding a dummy is a practical way around the issue but there is an easier way of setting the selected item. e.g.
b_m = wx.Menu()
b_m.AppendRadioItem(1, "Full Screen On")
b_m.AppendRadioItem(2, "Full Screen Off")
self.Bind(wx.EVT_MENU, self.OnFScrOn, id=1)
self.Bind(wx.EVT_MENU, self.OnFScrOff, id=2)
b_m.Check(2,True)

The last line sets menu item id 2 to True

I too would expect an index problem with list[i].Check(False)
as Joran mentioned above