简体   繁体   English

Python PyQt4 .setText拒绝变量?

[英]Python PyQt4 .setText refusing variables?

I would say I'm fairly decent with Python, but creating GUIs is a new concept for me. 我会说我对Python相当满意,但是创建GUI对我来说是一个新概念。 I've used Qt Creator to format the GUI and pyuic to convert the code from the file.ui. 我已经使用Qt Creator来格式化GUI,并使用pyuic来转换来自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. 我已经将大多数GUI编码了,但是在更新行编辑,按钮等标签的文本时遇到了这个问题。因此,该GUI具有从主程序打开的选项窗口,用户可以在其中指定某些参数。 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. 我正在导入config.py文件,其中有一个包含参数字符串的变量。 These are formatted and set alongside all other labels etc. But there not being set for some reason. 它们被格式化并与所有其他标签等一起设置。但是由于某种原因未设置。

config.py config.py

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

A nested function of mainProgram.py used to set the text of the labels etc. mainProgram.py的嵌套函数,用于设置标签等的文本。

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: 造成问题的原因是,尽管config.py文件已被修改,但它不会由python自动重新加载,为了强制执行该操作,您必须使用reload ,具体情况如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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