1

我尝试仅遵循本指南的第一部分:http: //talk.maemo.org/archive/index.php/t-39879.html 以仅使 hello world 部分起作用。它没有

这是错误

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_ ()
4

1 回答 1

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_()
于 2012-12-28T22:13:27.733 回答