如何在PyQt中使用pyqtSignal连接两个窗口?
我创建了一个简单的应用程序,里面有两个窗口,我想通过按钮把它们连接起来,使用的是pyqtSignal。但是当我运行这个应用程序时,出现了一个信息:“TypeError: pyqtSignal必须绑定到一个QObject,而不是'Form1'”。
from PyQt4 import QtCore, QtGui
import form2
import sys
from functools import partial
from PyQt4.QtCore import pyqtSignal
from lib2to3.fixer_util import String
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)
class Form1(QtGui.QMainWindow):
Form2Signal = pyqtSignal(str, str)
def __init__(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(319, 147)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.bt_form1 = QtGui.QPushButton(self.centralwidget)
self.bt_form1.setGeometry(QtCore.QRect(110, 30, 101, 41))
self.bt_form1.setObjectName(_fromUtf8("bt_form1"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 319, 21))
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.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
self.bt_form1.clicked.connect(self.Form2Signal.emit(String))
MainWindow.setWindowTitle(_translate("MainWindow", "Form_1", None))
self.bt_form1.setText(_translate("MainWindow", "FORM_1", None))
class Form2(QtGui.QMainWindow):
Form1Signal = pyqtSignal(str, str)
def __init__(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(319, 147)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.bt_form2 = QtGui.QPushButton(self.centralwidget)
self.bt_form2.setGeometry(QtCore.QRect(110, 30, 101, 41))
self.bt_form2.setObjectName(_fromUtf8("bt_form2"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 319, 21))
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.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
self.bt_form2.clicked.connect(self.Form1Signal.emit)
MainWindow.setWindowTitle(_translate("MainWindow", "Form_2", None))
self.bt_form2.setText(_translate("MainWindow", "FORM_2", None))
class MainWidget(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWidget, self).__init__(parent)
self.stack = QtGui.QStackedWidget()
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.stack)
self.form1 = Form1(self)
self.form2 = Form2(self)
self.stack.addWidget(self.form1)
self.stack.addWidget(self.form2)
self.form1.Form2Signal.connect(partial(self.stack.setCurrentWidget,self.form2))
self.form2.Form1Signal.connect(partial(self.stack.setCurrentWidget,self.form1))
self.stack.setCurrentWidget(self.form1)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
w = MainWidget()
w.show()
app.exec_()
sys.exit()
1 个回答
0
在Form1
类的__init__
方法里加上super(Form1, self).__init__()
这行代码。
对Form2
也做同样的操作。