如何在Qt Designer中对“hello world”程序进行故障排除?

时间:2012-12-28 16:39:00

标签: python qt pyqt

我试着遵循本指南的第一部分: http://talk.maemo.org/archive/index.php/t-39879.html 只让你好世界的一部分工作。它没有

这是错误

Traceback (most recent call last):
  File "helloworld2.py", line 69, in <module>
    mainWindow = MainWin(None, "main window")
  File "helloworld2.py", line 49, in __init__
    self.setupUi(rootWidget)
  File "helloworld2.py", line 25, in setupUi
    MainWindow.setCentralWidget(self.centralwidget)
AttributeError: 'QWidget' object has no attribute 'setCentralWidget'

这应该有效吗(调整后),还是应该采用完全不同的路线? 如果它应该正常工作,那么要解决的问题是什么?

以下是.py文件的组成部分。我不确定我是否正确输入,我不确定我是否理解输入它的正确方法,因此它与文件中的显示方式相同。如果这看起来不正确,我会再试一次。

from PyKDE4 import kdecore
from PyKDE4 import kdeui
from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(800, 400)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(80, 110, 271, 51))
        font = QtGui.QFont()
        font.setPointSize(24)
        self.label.setFont(font)
        self.label.setObjectName(_fromUtf8("label"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 20))
        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):
    MainWindow.setWindowTitle(kdecore.i18n(_fromUtf8("MainWindow")))
    self.label.setText(kdecore.i18n(_fromUtf8("Hello World!")))


if __name__ == '__main__':
    import sys
    global app
class MainWin(kdeui.KMainWindow, Ui_MainWindow):
    def __init__ (self, *args):
        kdeui.KMainWindow.__init__ (self)
        rootWidget = QtGui.QWidget(self)
        self.setupUi(rootWidget)
        self.resize(640, 480)
        self.setCentralWidget(rootWidget)

appName     = "default"
catalog     = ""
programName = kdecore.ki18n("default")
version     = "1.0"
description = kdecore.ki18n("Default Example")
license     = kdecore.KAboutData.License_GPL
copyright   = kdecore.ki18n("unknown")
text        = kdecore.ki18n("none")
homePage    = ""
bugEmail    = "email"

aboutData   = kdecore.KAboutData(appName, catalog, programName, version, description,
                          license, copyright, text, homePage, bugEmail)
kdecore.KCmdLineArgs.init(sys.argv, aboutData)

app = kdeui.KApplication()
mainWindow = MainWin(None, "main window")
mainWindow.show()
app.lastWindowClosed.connect(app.quit)
app.exec_ ()

1 个答案:

答案 0 :(得分:0)

看起来你的代码有点混乱,看看这是否适合你并从那里开始使用它:

#!/usr/bin/env python

from PyKDE4 import kdecore, kdeui
from PyQt4 import QtCore, QtGui

appName     = "default"
catalog     = ""
programName = kdecore.ki18n("default")
version     = "1.0"
description = kdecore.ki18n("Default Example")
license     = kdecore.KAboutData.License_GPL
copyright   = kdecore.ki18n("unknown")
text        = kdecore.ki18n("none")
homePage    = ""
bugEmail    = "email"
aboutData   = kdecore.KAboutData(appName, catalog, programName, version, description,
                          license, copyright, text, homePage, bugEmail)

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(800, 400)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(80, 110, 271, 51))
        font = QtGui.QFont()
        font.setPointSize(24)
        self.label.setFont(font)
        self.label.setObjectName(_fromUtf8("label"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 20))
        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):
        MainWindow.setWindowTitle(kdecore.i18n(_fromUtf8("MainWindow")))
        self.label.setText(kdecore.i18n(_fromUtf8("Hello World!")))

class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.setupUi(self)

if __name__ == "__main__":
    import  sys

    kdecore.KCmdLineArgs.init(sys.argv, aboutData)
    app = kdeui.KApplication()
    main = MainWindow()
    main.resize(640, 480)
    main.show()
    app.lastWindowClosed.connect(app.quit)
    app.exec_()