简体   繁体   中英

Change text of QLineEdit from another class

I have 2 classes : MainWindow() and ModelSelection().

I would like to access a QLineEdit which is declared in MainWindow() from ModelSelection() in order to change the text of the QLineEdit thanks to the method setText() .

mainWindow.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

It returns : AttributeError: 'module' object has no attribute 'fileLine'

You are confusing class and module.
The module mainWindow.py contain a class MainWindow . The attribute fileLine belongs to the class, not the module.

Anyway, you shouldn't have to import mainWindow in modelSelection.py .
You're already importing modelSection in mainWindow.py , so my guess is that mainWindow is the parent of modelSelection .
In this case, you can check this question: Getting container/parent object from within python

I presume the rest of your code is working and you have only provided minimal information. Add a 'self' in this line.

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

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