我使用2个窗口创建简单的应用程序,我希望使用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()
答案 0 :(得分:0)
在super(Form1, self).__init__()
课程的__init__
方法中添加Form1
。对Form2