2

我正在 python 中创建一个应用程序,其中前端是在 pyqt4 中制作的,后端是在 twisted 中制作的。当我尝试运行代码时,显示函数会引发错误。

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() 函数会抛出错误。

4

1 回答 1

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()
于 2013-07-02T02:55:30.680 回答