Esempi di livello base di PySide
Introduzione
Questa pagina contiene degli esempi di base di gestione della GUI con PySide adatti ai principianti. Gli Esempi di livello medio di PySide e gli Esempi di livello avanzato di PySide sono contenuti nelle rispettive pagine.
I nuovi arrivati alla programmazione GUI possono inciampare sulla parola "widget". Al di fuori dell'ambiente informatico di solito viene inteso come
"un piccolo gadget o un dispositivo meccanico, in particolare qualcosa il cui nome è sconosciuto o non specificato"
Nei lavori di GUI come PySide il termine "widget" è sovente utilizzato per riferirsi agli elementi visibili della GUI, cioè le finestre, i dialoghi e le funzioni di input o output. Tutti gli elementi visibili di PySide sono chiamati widget, e, per coloro che sono interessati, discendono tutti da una classe genitore comune: QWidget. Oltre agli elementi visibili PySide offre anche dei widget per il networking, XML, multimedia, e l'integrazione di database.
Un widget che non è incorporato in un widget genitore viene chiamato finestra, e di solito le finestre hanno una struttura e una barra del titolo. I tipi più comuni di finestre sono la "finestra principale" (dalla Classe QMainWindow) e le varie sottoclassi di finestre di dialogo (dalla classe QDialog). La finestra QDialog è modale (cioè l'utente non può fare nulla al di fuori della finestra di dialogo mentre essa è aperta), invece la finestra QMainWindow è non-modale e permette all'utente di interagire in parallelo con altre finestre.
Questa guida è una lista di scorciatoie per fare lavorare rapidamente un programma PySide sotto FreeCAD, non è finalizzata a insegnare Python o PySide. Alcuni siti che lo fanno sono:
- PySide tutorial in zetcode.com
- PySide/PyQt Tutorial in PythonCentral.io
- PySide 1.0.7 Reference in Srinikom.github.io (notare che questo è un manuale di riferimento, non un tutorial)
Dichiarazione Import
PySide non viene caricato automaticamente con Python, prima di usarlo deve essere richiamato. Il seguente comando:
from PySide import QtGui, QtCore
causes the 2 parts of PySide to be loaded - QtGui holds classes for managing the Graphic User Interface while QtCore contains classes that do not directly relate to management of the GUI (e.g. timers and geometry). Although it is possible to only import the one that is needed, generally they are both needed and both imported.
Note: the 'import' statement is not repeated in each code snippet below, it is assumed that it is already in the Python file.
Simplest Example
The simplest interaction with PySide is to present a message to the user which they can only accept:
reply = QtGui.QMessageBox.information(None,"","Houston, we have a problem")
Yes or No Query
The next most simple interaction is to ask for a yes/no answer:
reply = QtGui.QMessageBox.question(None, "", "This is your chance to answer, what do you think?",
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
# this is where the code relevant to a 'Yes' answer goes
pass
if reply == QtGui.QMessageBox.No:
# this is where the code relevant to a 'No' answer goes
pass
Enter Text Query
The next code snippet asks the user for a piece of text - note this can be any key on the keyboard really:
reply = QtGui.QInputDialog.getText(None, "Ouija Central","Enter your thoughts for the day:")
if reply[1]:
# user clicked OK
replyText = reply[0]
else:
# user clicked Cancel
replyText = reply[0] # which will be "" if they clicked Cancel
Remember that even if the user enters only digits, "1234" for example, they are strings and must be converted to number representation with either of the following:
anInteger = int(userInput) # to convert to an integer from a string representation
aFloat = float(userInput) # to convert to a float from a string representation