【问题标题】:wxPython Menu - deselect all Radio Buttons (wx.RADIO_ITEM)wxPython 菜单 - 取消选择所有单选按钮 (wx.RADIO_ITEM)
【发布时间】:2015-08-26 08:30:11
【问题描述】:

我正在开发我的第一个 wxpython 应用程序。它是命令行实用程序的 GUI,需要连接到 samba 共享。现在我想将服务器选择添加到菜单中作为单选按钮。 我将 ConfigParser 用于设置 ini 文件,该文件还包含默认服务器的配置以及 它应该连接的服务器。

现在我想取消选择所有单选按钮,如果没有选择默认服务器并且程序没有连接到 启动。

这是我将 RADIO_ITEMS 添加到菜单的代码: (我知道这可能会做得更好,但我仍在学习,我很高兴它有效,欢迎任何建议)

# 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))

【问题讨论】:

    标签: python python-2.7 radio-button wxpython menuitem


    【解决方案1】:
    [menu.Check(False) for menu in my_list]
    

    不要使用 list 作为你的变量名,我也希望你用这段代码得到一个 IndexError ....

    【讨论】:

    • 这导致:AttributeError: 'str' object has no attribute 'Check' 代码工作正常,没有发现任何错误。 Check(True) 也有效,但 Check(False) 似乎被忽略了。为菜单项创建变量的更好解决方案是什么?
    • 谢谢,似乎无法禁用菜单中的所有单选按钮。我的解决方案,添加另一个(未连接),然后禁用这个。它仍然可以在函数中进行检查,但它显示为灰色,因此用户无法与之交互。
    【解决方案2】:

    你是对的 Sonic,看来你必须在菜单 RadioItems 中检查了一些东西,所以添加一个虚拟对象是解决这个问题的一种实用方法,但有一种更简单的方法来设置所选项目。例如
    b_m = wx.Menu()
    b_m.AppendRadioItem(1, "全屏打开")
    b_m.AppendRadioItem(2, "全屏关闭")
    self.Bind(wx.EVT_MENU, self.OnFScrOn, id=1)
    self.Bind(wx.EVT_MENU, self.OnFScrOff, id=2)
    b_m.Check(2,True)

    最后一行将菜单项 id 2 设置为 True

    我也希望索引问题 list[i].Check(False)
    正如上面提到的乔兰

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-22
      • 2012-02-21
      • 1970-01-01
      • 2012-08-16
      • 2020-03-26
      • 2011-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多