【发布时间】: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