我从http://www.pythoncentral.io/pyside-pyqt-tutorial-interactive-widgets-and-layout-containers/下载了下面的脚本
我收到以下错误消息:name error:name'QApplication'未定义
我加了剧本的前两行。
那没用。
我想也许我不应该安装qt。但是当我试图运行PyQt4-4.10.3-gpl-Py2.7-Qt4.8.5-x32.exe时,程序告诉我它已经安装好了。
有人有什么建议吗?
马克

# copied from http://www.pythoncentral.io/pyside-pyqt-tutorial-interactive-widgets-and-layout-containers/
# Every Qt application must have one and only one QApplication object;
# it receives the command line arguments passed to the script, as they
# can be used to customize the application's appearance and behavior

import sys
from PyQt4 import QtGui, QtCore
#import PyQt4.QtGui, PyQt4.QtCore
qt_app = QApplication(sys.argv)

class AbsolutePositioningExample(QWidget):
    ''' An example of PySide absolute positioning; the main window
        inherits from QWidget, a convenient widget for an empty window. '''
    def __init__(self):
        # Initialize the object as a QWidget
        QWidget.__init__(self)

        # We have to set the size of the main window
        # ourselves, since we control the entire layout
        self.setMinimumSize(400, 185)
        self.setWindowTitle('Dynamic Greeter')

        # Create the controls with this object as their parent and set
        # their position individually; each row is a label followed by
        # another control

        # Label for the salutation chooser
        self.salutation_lbl = QLabel('Salutation:', self)
        self.salutation_lbl.move(5, 5) # offset the first control 5px
                                       # from top and left
        self.salutations = ['Ahoy',
                            'Good day',
                            'Hello',
                            'Heyo',
                            'Hi',
                            'Salutations',
                            'Wassup',
                            'Yo']
        # Create and fill the combo box to choose the salutation
        self.salutation = QComboBox(self)
        self.salutation.addItems(self.salutations)

        # Allow 100px for the label and 5px each for borders at the
        # far left, between the label and the combobox, and at the far
        # right
        self.salutation.setMinimumWidth(285)
        # Place it five pixels to the right of the end of the label
        self.salutation.move(110, 5)

        # The label for the recipient control
        self.recipient_lbl = QLabel('Recipient:', self)
        # 5 pixel indent, 25 pixels lower than last pair of widgets
        self.recipient_lbl.move(5, 30)

        # The recipient control is an entry textbox
        self.recipient = QLineEdit(self)
        # Add some ghost text to indicate what sort of thing to enter
        self.recipient.setPlaceholderText(""e.g. 'world' or 'Matey'"")
        # Same width as the salutation
        self.recipient.setMinimumWidth(285)
        # Same indent as salutation but 25 pixels lower
        self.recipient.move(110, 30)

        # The label for the greeting widget
        self.greeting_lbl = QLabel('Greeting:', self)
        # Same indent as the others, but 45 pixels lower so it has
        # physical separation, indicating difference of function
        self.greeting_lbl.move(5, 75)

        # The greeting widget is also a label
        self.greeting = QLabel('', self)
        # Same indent as the other controls
        self.greeting.move(110, 75)

        # The build button is a push button
        self.build_button = QPushButton('&Build Greeting', self)

        # Place it at the bottom right, narrower than
        # the other interactive widgets
        self.build_button.setMinimumWidth(145)
        self.build_button.move(250, 150)

    def run(self):
        # Show the form
        self.show()
        # Run the Qt application
        qt_app.exec_()

# Create an instance of the application window and run it
app = AbsolutePositioningExample()
app.run()

最佳答案

如果您按顺序阅读本教程,您会看到系列中的the previous article在每个片段的开头显示了您需要分割的部分,以使其成为一个可运行的程序。作者这样做显然是为了让PyQt和PySide都能使用相同的代码。
因此,如果您使用的是PyQt4,则需要添加以下内容:

# Allow access to command-line arguments
import sys

# SIP allows us to select the API we wish to use
import sip

# use the more modern PyQt API (not enabled by default in Python 2.x);
# must precede importing any module that provides the API specified
sip.setapi('QDate', 2)
sip.setapi('QDateTime', 2)
sip.setapi('QString', 2)
sip.setapi('QTextStream', 2)
sip.setapi('QTime', 2)
sip.setapi('QUrl', 2)
sip.setapi('QVariant', 2)

# Import all of Qt
from PyQt4.Qt import *

如果PySide:
# Allow access to command-line arguments
import sys

# Import the core and GUI elements of Qt
from PySide.QtCore import *
from PySide.QtGui import *

在显示这个样板文件的框下面,有一个很好的、可读的解释,说明了它的含义以及为什么需要这样做。
不过,我建议你,如果你想从一个教程中学到东西,你应该从一开始就努力向前,而不是从中间开始,试图找出你一路上错过了什么。
如果您只是执行from PyQt4 import QtGui, QtCore而不是从中导入*,则这些模块中的名称可用,但仅作为限定名。也就是说,您必须编写QApplication,而不是QtCore.QApplication
如果您不了解这一区别,请阅读正式的Python教程中的Modules或类似内容,了解导入的工作原理。

关于python - QApplication'未定义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20274856/

10-10 07:11