wxPython:动态启用和禁用小部件,从 csv 文件获取设置
我有一个小程序,其中包含许多小部件,例如复选框、textctrls、statictexts...
我所有小部件的值都存储在字典中。我正在保存设置,例如将此字典保存在 csv 文件中。当我从此文件导入设置并更新我的字典时,我的所有小部件都应通过调用“def enable_controls”根据字典中的值启用或禁用。但小部件始终处于启用状态,即使该值为“False”。
这是代码片段:
def enable_controls(self):
self.Checkbox1.SetValue(bool(config.StartValues['Checkbox1']))
self.Checkbox1TextCtrl.Enable(bool(config.StartValues['Checkbox1']))
self.Checkbox1StaticText.Enable(bool(config.StartValues['Checkbox1']))
self.Checkbox2.SetValue(bool(config.StartValues['Checkbox2']))
self.Checkbox2TextCtrl.Enable(bool(config.StartValues['Checkbox2']))
self.Checkbox2StaticText.Enable(bool(config.StartValues['Checkbox2']))
当我不动态分配值时,它会起作用:
def enable_controls(self):
self.Checkbox1.SetValue(False)
self.Checkbox1TextCtrl.Enable(False)
self.Checkbox1StaticText.Enable(False)
self.Checkbox2.SetValue(False)
self.Checkbox2TextCtrl.Enable(False)
self.Checkbox2StaticText.Enable(False)
我是否正确地进行了 bool 类型转换?
编辑:这是我从 csv 文件中写入和读取的方式:
def onButtonSave(self, event):
import csv
getValues(self, StartValues)
writer = csv.writer(open('Test.csv', 'wb'))
for key, value in sorted(config.StartValues.items()):
writer.writerow([key, value])
def onButtonLoad(self, event):
import csv
reader = csv.reader(open('Test.csv', 'rb'))
config.StartValues = dict(x for x in reader)
enable_controls(self)
I have a small program with many widgets like checkboxes, textctrls, statictexts,...
The values of all my widgets are stored in a dictionary. I'm saving the settings, e.g. this dictionary in a csv file. When I'm importing the settings from this file and update my dictionary, all my widgets should be enabled or disabled depending on the value in the dictionary by calling "def enable_controls". But the widgets are always enabled, even if the value is "False".
Here is the code snippet:
def enable_controls(self):
self.Checkbox1.SetValue(bool(config.StartValues['Checkbox1']))
self.Checkbox1TextCtrl.Enable(bool(config.StartValues['Checkbox1']))
self.Checkbox1StaticText.Enable(bool(config.StartValues['Checkbox1']))
self.Checkbox2.SetValue(bool(config.StartValues['Checkbox2']))
self.Checkbox2TextCtrl.Enable(bool(config.StartValues['Checkbox2']))
self.Checkbox2StaticText.Enable(bool(config.StartValues['Checkbox2']))
When I don't assign the value dynamically, it works:
def enable_controls(self):
self.Checkbox1.SetValue(False)
self.Checkbox1TextCtrl.Enable(False)
self.Checkbox1StaticText.Enable(False)
self.Checkbox2.SetValue(False)
self.Checkbox2TextCtrl.Enable(False)
self.Checkbox2StaticText.Enable(False)
Am I doing the type conversion to bool correctly?
Edit: This is how I write and read from my csv file:
def onButtonSave(self, event):
import csv
getValues(self, StartValues)
writer = csv.writer(open('Test.csv', 'wb'))
for key, value in sorted(config.StartValues.items()):
writer.writerow([key, value])
def onButtonLoad(self, event):
import csv
reader = csv.reader(open('Test.csv', 'rb'))
config.StartValues = dict(x for x in reader)
enable_controls(self)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如我在评论中所说,不要使用
bool(config.StartValues['Checkbox2'])
进行数据转换。bool
对于任何不计算结果为0
、0.0
或空序列或映射。在这种情况下,像"True"
和"False"
这样的字符串都将计算为True
使用字符串比较或类似的方法会更好,但要注意如果您让用户编辑文件,就会减少用户输入的拼写错误!
编辑:使用示例...
As I said up in the comments, don't use
bool(config.StartValues['Checkbox2'])
to do the data conversion.bool
returnsTrue
for anything that does not evaluate to0
,0.0
or an empty sequence or map. In this case, strings like"True"
and"False"
will both evaluate toTrue
Using string comparison or similar would be better, but beware of user-introduced typos if you let them edit the files!
Edit: use example...