我正在python中创建一个应用程序,其中前端是在pyqt4中制作的,后端是在扭曲中制作的。当我尝试运行代码时,show函数会抛出错误。
Traceback (most recent call last):
File "C:\Users\ABHISHEK\Desktop\teest\cort1.py", line 358, in <module>
MainWindow.show(reactor)
TypeError: QWidget.show(): first argument of unbound method must have type 'QWidget'
代码:
__name__ == "__main__":
app = QtGui.QApplication(sys.argv)
try:
import qt4reactor
except ImportError:
from twisted.internet import qt4reactor
qt4reactor.install()
from twisted.internet import reactor
MainWindow=QtGui.QMainWindow
ui = Ui_MainWindow(reactor)
ui.setupUi(MainWindow)
MainWindow.show()
reactor.run()
这是Ui_MainWindow类的代码:
class Ui_MainWindow(QtGui.QMainWindow):
def __init__(self,reactor, parent=None):
super(Ui_MainWindow,self).__init__(parent)
self.reactor=reactor
self.pf = Factory()
self.pf.protocol = Protocol
self.reactor.listenTCP(3609, self.pf) # listen on port 1234
def setupUi(self,MainWindow):
MainWindow=QtGui.QMainWindow()
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(903, 677)
self.centralwidget = QtGui.QWidget(MainWindow)
#then rest of the ui components.
任何人都可以告诉我为什么show()函数会抛出错误。
答案 0 :(得分:3)
您没有实例化QtGui.QMainWindow
。我修改了你的代码:(见评论)
from PyQt4 import QtGui, QtCore
import sys
from twisted.internet.protocol import Factory, Protocol
_fromUtf8 = QtCore.QString.fromUtf8
class Ui_MainWindow(QtGui.QMainWindow):
def __init__(self,reactor, parent=None):
super(Ui_MainWindow,self).__init__(parent)
self.reactor=reactor
self.pf = Factory()
self.pf.protocol = Protocol
self.reactor.listenTCP(3609, self.pf) # listen on port 1234
def setupUi(self,MainWindow):
#MainWindow=QtGui.QMainWindow() # <-- Just use passed MainWindow.
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(903, 677)
self.centralwidget = QtGui.QWidget(MainWindow)
#then rest of the ui components.
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
try:
import qt4reactor
except ImportError:
from twisted.internet import qt4reactor
qt4reactor.install()
from twisted.internet import reactor
MainWindow = QtGui.QMainWindow() # <-- Instantiate QMainWindow object.
ui = Ui_MainWindow(reactor)
ui.setupUi(MainWindow)
MainWindow.show()
reactor.run()