简体   繁体   中英

Python PyQt4 .setText refusing variables?

I would say I'm fairly decent with Python, but creating GUIs is a new concept for me. I've used Qt Creator to format the GUI and pyuic to convert the code from the file.ui.

I have most of the GUI coded, but I'm having this problem updating the text for labels for line edits, push buttons etc. So this GUI has an options window that opens from the main program where the user can specify certain parameters. Currently, I open the options, set the values, close, reopen the option window, and the text has not changed to the new values which are variables. Plain strings do work however. Variables will 'stick,' only if the program is restarted.

I'm importing a config.py file where there is a variable containing the string of parameters. These are formatted and set alongside all other labels etc. But there not being set for some reason.

config.py

configAttrs="clientid,oauth,123,source,123"

A nested function of mainProgram.py used to set the text of the labels etc.

def retranslateUi(self, OptionsWindow):
    OptionsWindow.setWindowTitle(_translate("OptionsWindow", "OptionsWindow", None))
    self.label_MainOptions.setText(_translate("OptionsWindow", "Options", None))


    confs = config.configAttrs.split(',')
    clientid = str(confs[0])
    oauth =  str(confs[1])
    cache = str(confs[2])
    heightAdjust = str(confs[4])

    #does NOT work when reopening options window
    #does work with restart
    self.lineEdit_ClientID.setText(_translate("OptionsWindow", clientid, None))

    #does NOT work when reopening options window
    #does work with restart
    self.lineEdit_ClientID.setText('{0}'.format(clientid))

    #does work when reopening options window
    #does work with restart
    self.lineEdit_ClientID.setText(_translate("OptionsWindow", 'string_clientid', None))

Shortened the code above.*

The problem is caused because although the config.py file is modified this is not automatically reloaded by python, in order to force it you must use reload , in your case:

def retranslateUi(self, OptionsWindow):
    [...]
    reload(config)
    confs = config.configAttrs.split(',')
    [...]

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