QApplication " n'est pas défini
J'ai téléchargé le script ci-dessous à partir de http://www.pythoncentral.io/pyside-pyqt-tutorial-interactive-widgets-and-layout-containers/
Je reçois le message d'erreur suivant: NameError: name 'QApplication" n'est pas défini
J'ai ajouté les deux premières lignes du script.
Qui n'a pas aidé.
J'ai pensé que peut-être que je ne doit pas avoir qt installé. Mais quand j'ai essayé d'exécuter PyQt4-4.10.3-gpl-Py2.7-Qt4.8.5-x32.exe le programme m'a dit qu'il était déjà installé.
Quelqu'un aurait-il des suggestions?
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()
Ce code ne semble pas être un exemple complet, mais plutôt un fragment de code. Le tutoriel explique probablement ce que vous devez ajouter à chaque fragment pour le rendre exécutable dans l'une des étapes antérieures de vous sauter dessus. Si non, vous devriez commentaire sur l'article pour laisser savoir à l'auteur, il y a un problème, et il peut vous dire comment le réparer.
OriginalL'auteur user235218 | 2013-11-28
Vous devez vous connecter pour publier un commentaire.
Si vous lisez le tutoriel dans l'ordre, vous verriez que l'article précédent dans la série a montré les trucs que vous devez partie au début de chaque fragment pour en faire un exécutable du programme. L'auteur n'a apparemment cela de sorte que le même code peut être utilisé à la fois avec PyQt et PySide.
Donc, si vous êtes à l'aide de PyQt4, vous aurez besoin d'ajouter ceci:
Si PySide:
Ci-dessous la case indiquant votre ce standard, il y a un beau, lisible explication de ce que tout cela signifie, et pourquoi vous avez besoin de le faire.
Cependant, je suggère que si vous essayez d'apprendre à partir d'un tutoriel, vous commencer au début et travailler vers l'avant, au lieu de commencer au milieu et à essayer de comprendre ce que vous avez manqué le long du chemin.
Si vous venez de le faire
from PyQt4 import QtGui, QtCore
au lieu de l'importation de*
d'eux, les noms de ces modules sont disponibles, mais seulement en tant que noms qualifiés. C'est, au lieu deQApplication
, vous devez écrireQtCore.QApplication
.Si vous ne comprenez pas la différence, lire Les Modules de dans l'officiel de Python tutoriel, ou quelque chose d'équivalent, pour apprendre les importations de travail.
OriginalL'auteur abarnert
Je suppose que l'importation est mauvais, il doit être:
OriginalL'auteur Fenikso