簡體   English   中英

Python PyQt4 .setText拒絕變量?

[英]Python PyQt4 .setText refusing variables?

我會說我對Python相當滿意,但是創建GUI對我來說是一個新概念。 我已經使用Qt Creator來格式化GUI,並使用pyuic來轉換來自file.ui的代碼。

我已經將大多數GUI編碼了,但是在更新行編輯,按鈕等標簽的文本時遇到了這個問題。因此,該GUI具有從主程序打開的選項窗口,用戶可以在其中指定某些參數。 當前,我打開選項,設置值,關閉,重新打開選項窗口,並且文本尚未更改為變量的新值。 純字符串確實可以工作。 僅當程序重新啟動時,變量才會“粘住”。

我正在導入config.py文件,其中有一個包含參數字符串的變量。 它們被格式化並與所有其他標簽等一起設置。但是由於某種原因未設置。

config.py

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

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))

縮短了上面的代碼。*

造成問題的原因是,盡管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