【问题标题】:Change text of QLineEdit from another class从另一个类更改 QLineEdit 的文本
【发布时间】:2015-07-02 13:10:27
【问题描述】:

我有 2 个类:MainWindow() 和 ModelSelection()。

我想从 ModelSelection() 访问在 MainWindow() 中声明的 QLineEdit,以便通过 setText() 方法更改 QLineEdit 的文本。

ma​​inWindow.py

from modelSelection import ModelSelection

def __init__(self, workingDir, filename, mode, tabAnalysis, parent=None):
    super(MainWindow,self).__init__(parent)
    self.fileLine = QLineEdit()

modelSelection.py

import mainWindow    

def openModelDialog(self):
    self.filename = QFileDialog.getOpenFileName(self, "Open File",filePath,"(*.txt)")

    if self.filename:
        mainWindow.fileLine.setText(self.filename[0])
    return self.filename

返回:AttributeError: 'module' object has no attribute 'fileLine'

【问题讨论】:

标签: python python-3.x pyqt pyqt5


【解决方案1】:

您混淆了类和模块。
模块mainWindow.py 包含一个类MainWindow。属性fileLine 属于类,而不是模块。

无论如何,您不必将mainWindow 导入modelSelection.py
你已经在mainWindow.py 中导入了modelSection,所以我的猜测是 mainWindowmodelSelection 的父级。
在这种情况下,你可以检查这个问题:Getting container/parent object from within python

【讨论】:

    【解决方案2】:

    我认为您的其余代码正在运行,而您只提供了最少的信息。在这一行中添加一个“自我”。

    self.mainWindow.fileLine.setText(self.filename[0])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      • 1970-01-01
      相关资源
      最近更新 更多