Adding PyQt Gui Elements from Another File

I am learning PyQt and coming from webdesign, so excuse this question, which should have a very obvious answer. Therefore, I am creating a PyQt application, and I would like to extend the methods to multiple files to fit different parts of the GUI. How can I access the text field located in the A..py file from the file.

#fileA.py
import sys
from PyQt4 import QtGui, QtCore
from gui1 import Ui_MainWindow
import fileB

class MyApp(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)


if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        window = MyApp()
        window.show()

        #This works all fine
        def pressed():
            window.plainTextEdit.appendPlainText("Hello")


        window.pushButton.pressed.connect(pressed)


        window.button2.pressed.connect(fileB.func3)
        sys.exit(app.exec_())    

Now in this file I would like to use the text box from file.pp

#fileB.py
import fileA

    #How do I access window.plainTextEdit from fileA.py
def func3():
    print "hello"
    fileA.window.plainTextEdit.appendPlainText("Hello")

What am I doing wrong? What would be the best way to extend functionality to multiple files if not for this?

Thanks for taking the time to read this.

+3
source share
2 answers

You can use Python class inheritance, for example:

fileA.py

import sys
from PyQt4 import QtGui, QtCore
from gui1 import Ui_MainWindow
import fileB

class MyApp(fileB.MyApp, QtGui.QMainWindow):
  def __init__(self):
     self.MyMethod()
     # Should print 'foo'

fileB.py

import sys
from PyQt4 import QtGui, QtCore
from gui1 import Ui_MainWindow

class MyApp(QtGui.QMainWindow):
  def MyMethod(self):
    print 'foo'
+2

, -, if __name__ == "__main__" , fileA.py, fileA.window . , : , __name__ "__main__", . fileA.py, QApplication window, window.plainTextEdit. , MyApp fileB. , , MyApp, , , fileB.py . , .

, ;

window = MyApp()
window.plainTextEdit.appendPlainText("Hello")

fileB, .

+1

All Articles