【发布时间】:2015-07-02 14:26:57
【问题描述】:
我有一个GUI(使用 PyQt5 和 Python3.4 创建)。我有一对combobox:第一个是汽车品牌,第二个是所选品牌的车型。
我有一个INI file 来保存在GUI 中输入的最后一个值,所以当我重新打开GUI 时,会自动输入最后一个值/参数。
这适用于我的所有其他参数,combobox 除外。
我在INI file中写了currentIndex,当我关闭我的GUI时,INI文件中写入了正确的索引,但是当我再次打开GUI时,索引变为0。
这就是我的代码的样子:
def comboSelect(self):
config = configparser.ConfigParser()
config.read('D:\File\save.ini')
self.indexModel = ui.dmpModele.currentIndex()
config.set('de_sec', 'dmp_modele', str(self.indexModel))
with open('D:\File\save.ini', 'w') as configfile:
config.write(configfile) # write the index in the INI file
if __name__ == "__main__":
config = configparser.ConfigParser()
config.read('D:\File\save.ini')
p.indexModel = config.get('de_sec', 'dmp_modele') # get the value of index from INI file
ui.dumperModele.setCurrentIndex(int(p.indexModel)) # pass the index to combo box
我错过了什么?
【问题讨论】:
-
对我来说很好。您的代码的其他部分必须重置组合框的索引。
-
@ekhumoro 我曾经有一个 setCurrentIndex 但我删除了它。我会寻找导致问题的其他原因。
-
@ekhumoro 我似乎找不到任何正在重置组合框索引的东西。你有什么想法可以这样做吗?
标签: python combobox ini pyqt5 qcombobox