PyQT:如何从外部函数访问MainWindow小部件?

时间:2014-08-15 15:15:47

标签: python function user-interface pyqt

例如,单击主窗口中的按钮调用test()函数将一些文本添加到主窗口中的textEdit框

#!/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_())

我知道我不应该使用生成的文件,只是试图找出如何从该类之外的函数引用主窗口对象。

2 个答案:

答案 0 :(得分:1)

我认为最简单的方法是使test成为Ui_MainWindow的实例方法,并将MainWindow对象另存为Ui_MainWindow的实例变量。

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')

如果您真的希望test成为顶级函数,那么您还可以使用functools.partial在执行MainWindow回调时传递test对象:

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)

答案 1 :(得分:0)

可能的方法是将对象QtGui.QMainWindow传递给方法,使用test()将MainWindow对象传递给partial以传递您想要的参数;

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

此致