简体   繁体   中英

Load values from INI file to combo box

I have a GUI (created with PyQt5 & Python3.4 ). I have a pair of combobox : the first is brand of cars and the second one is models of the brand selected.

I have an INI file to save the last values entered in the GUI , so when I re open the GUI , the last values/parameters are automatically put. This works for all my other parameters, except for the combobox . I write the currentIndex in the INI file , when I close my GUI the correct index is written in the INI file, but when i open the GUI again, the index is changed to 0.

This is what my code looks like :

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

What have I missed ?

Okay I found it. It was a stupid mistake. In my

if __name__ == "__main__":

i was calling the functions before getting the values from the .ini file and so there were put to zero. That's it.

UPDATE : now the index is correct, but the name in the combobox is not. It doesn't change, even though I have set a setCurrentIndex with the correct index . How about that ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM