user3891489
user3891489

Reputation: 61

PyQT: How to access MainWindow widgets from an external function?

For example, clicking button in main window calls test() function to add some text to textEdit box in main window

#!/usr/bin/python
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'felch1.ui'
#
# Created: Fri Aug 15 15:19:31 2014
#      by: PyQt4 UI code generator 4.10.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

def test():
    MainWindow.textEdit.append('button pushed')


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(800, 600)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(30, 20, 131, 31))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.textEdit = QtGui.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(30, 80, 341, 441))
        self.textEdit.setObjectName(_fromUtf8("textEdit"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 25))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), test)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.pushButton.setText(_translate("MainWindow", "test", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

I know I shouldn't noodle with the generated file, just trying to figure out how to reference main window objects from a function outside that class.

Upvotes: 1

Views: 7032

Answers (2)

Bandhit Suksiri
Bandhit Suksiri

Reputation: 3460

The possible is pass object QtGui.QMainWindow to method do your want, pass the MainWindow object to test() by using partial to pass parameter your want;

    from PyQt4 import QtCore, QtGui
    from functools import partial
        .
        .
        .
    def test(MainWindow):
        MainWindow.textEdit.append('button pushed')

    def setupUi(self, MainWindow):
        .
        .
        .
        MainWindow.textEdit = QtGui.QTextEdit(self.centralwidget) # Pass to QMainWindows not self
        MainWindow.textEdit.setGeometry(QtCore.QRect(30, 80, 341, 441))
        MainWindow.textEdit.setObjectName(_fromUtf8("textEdit"))
        MainWindow.setCentralWidget(self.centralwidget)
        .
        .
        .
        self.retranslateUi(MainWindow)
        QtCore.QObject.connect(MainWindow.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), partial(test, MainWindow))
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        .
        .
        .

Regards,

Upvotes: 0

dano
dano

Reputation: 94961

I think the easiest way would be to make test an instance method of Ui_MainWindow, and save the MainWindow object as an instance variable of Ui_MainWindow, as well.

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(800, 600)
        self.main_window = MainWindow

        # blah blah

        self.textEdit = QtGui.QTextEdit(self)
        self.pushButton = QtGui.QPushButton(self)
        self.pushButton.clicked.connect(self.test)

    def test(self):
        self.main_window.textEdit.append('button pushed')

If you really wanted test to be a top-level function, you could also use functools.partial to pass the MainWindow object along when the test callback is executed:

from functools import partial

def test(MainWindow):
    MainWindow.textEdit.append('button pushed')


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(800, 600)

        # blah blah

        self.textEdit = QtGui.QTextEdit(self)
        self.pushButton = QtGui.QPushButton(self)
        test_func = partial(test, MainWindow)  # Calling test_func(arg) actually calls test(MainWindow, arg)
        self.pushButton.clicked.connect(test_func)

Upvotes: 1

Related Questions