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)
As I said up in the comments, don’t use
bool(config.StartValues['Checkbox2'])to do the data conversion.boolreturnsTruefor anything that does not evaluate to0,0.0or an empty sequence or map. In this case, strings like"True"and"False"will both evaluate toTrueUsing string comparison or similar would be better, but beware of user-introduced typos if you let them edit the files!
Edit: use example…