QApplication' ist nicht definiert
Ich heruntergeladen habe das script unten aus http://www.pythoncentral.io/pyside-pyqt-tutorial-interactive-widgets-and-layout-containers/
Ich bekomme folgende Fehlermeldung: NameError: name 'QApplication' ist nicht definiert
Habe ich noch die ersten beiden Zeilen des Skripts.
Das nicht helfen.
Ich dachte, vielleicht muss ich nicht haben-qt installiert. Aber wenn ich versuchte zu laufen PyQt4-4.10.3-gpl-Py2.7-Qt4.8.5-x32.exe das Programm sagte mir, es war bereits installiert.
Hat jemand irgendwelche Vorschläge?
marc
# 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()
Dass der code nicht angezeigt wird, um ein vollständiges Beispiel, sondern ein fragment von code. Das tutorial erklärt wahrscheinlich, was Sie brauchen, um hinzuzufügen, zu jedem fragment, um es lauffähig in einem der früheren Schritte, die Sie übersprungen. Wenn nicht, sollten Sie Kommentar auf den Artikel, damit der Autor weiß, gibt es ein problem, und so kann er Ihnen sagen, wie um es zu beheben.
InformationsquelleAutor user235218 | 2013-11-28
Du musst angemeldet sein, um einen Kommentar abzugeben.
Wenn Sie Lesen, durch das tutorial in Ordnung, würden Sie sehen, dass die vorherigen Artikel in der Serie zeigten die Sachen, die Sie brauchen, um Teil zu Beginn jedes fragments, um es zu einem lauffähigen Programm. Der Autor hat offensichtlich so, dass der gleiche code könnte verwendet werden, sowohl mit PyQt und PySide.
So, wenn Sie mit PyQt4, müssen Sie Folgendes hinzufügen:
Wenn PySide:
Unterhalb der box zeigen Ihr dieses boilerplate, es ist eine schöne, lesbare Erklärung, was das alles bedeutet und warum Sie es tun müssen.
Jedoch, ich würde vorschlagen, dass, wenn Sie versuchen zu lernen aus einem tutorial, starten Sie an den start und arbeiten Sie sich vorwärts, statt beginnend in der Mitte und versucht herauszufinden, was Sie verpasst auf dem Weg.
Wenn Sie gerade
from PyQt4 import QtGui, QtCore
anstatt Sie zu importieren*
von Ihnen, die Namen in diesen Modulen sind verfügbar, aber nur als qualifizierte Namen verwendet werden. Das heißt, anstelle vonQApplication
Sie haben zu schreibenQtCore.QApplication
.Wenn Sie nicht verstehen, den Unterschied, Lesen Module in die offizielle Python-tutorial oder etwas vergleichbares, um zu erfahren, wie die Importe Arbeit.
InformationsquelleAutor abarnert
Ich denke, das importieren ist falsch, es sollte sein:
InformationsquelleAutor Fenikso