Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accesing PyQt Gui elements from another file

Tags:

python

pyqt

I am learning PyQt and coming from webdesign, so excuse this question that must have very obvious answer.So I am building a PyQt application and I would like to spread methods to several files to correspond different parts of GUI. How can I access textbox locating in fileA.py from fileB.py. :

#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 textbox from fileA.py

#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 best way to spread functionality to multiple files if not this?

Thank you for taking time to read this.

like image 442
Marci22 Avatar asked Jan 27 '26 22:01

Marci22


1 Answers

You can take advantage of Python's class inheritance, like so:

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'
like image 53
Blender Avatar answered Jan 30 '26 13:01

Blender



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!