Übersichtknoll/python/material/SommerkursPython... · PyQT – Tutorial TU Dresden, 23.05.2011...
Transcript of Übersichtknoll/python/material/SommerkursPython... · PyQT – Tutorial TU Dresden, 23.05.2011...
![Page 1: Übersichtknoll/python/material/SommerkursPython... · PyQT – Tutorial TU Dresden, 23.05.2011 Sommerkurs Python - 05. GUI Folie 12 von XYZ . Hello World >>> import sys](https://reader031.fdocuments.in/reader031/viewer/2022022601/5b48d9367f8b9a824f8cdcb2/html5/thumbnails/1.jpg)
Übersicht
GUI
PyQT
Sommerkurs Python - 05. GUI TU Dresden, 23.05.2011 Folie 1 von XYZ
![Page 2: Übersichtknoll/python/material/SommerkursPython... · PyQT – Tutorial TU Dresden, 23.05.2011 Sommerkurs Python - 05. GUI Folie 12 von XYZ . Hello World >>> import sys](https://reader031.fdocuments.in/reader031/viewer/2022022601/5b48d9367f8b9a824f8cdcb2/html5/thumbnails/2.jpg)
GUI Graphical User Interface
Sommerkurs Python - 05. GUI TU Dresden, 23.05.2011 Folie 2 von XYZ
![Page 3: Übersichtknoll/python/material/SommerkursPython... · PyQT – Tutorial TU Dresden, 23.05.2011 Sommerkurs Python - 05. GUI Folie 12 von XYZ . Hello World >>> import sys](https://reader031.fdocuments.in/reader031/viewer/2022022601/5b48d9367f8b9a824f8cdcb2/html5/thumbnails/3.jpg)
GUI - Graphical User Interface
Ø Wikipedia sagt: Eine grafische Benutzeroberfläche ist eine Software-Komponente, die dem Benutzer eines Computers die Interaktion mit der Maschine über grafische Symbole erlaubt. Die Darstellungen und Elemente (Arbeitsplatz, Symbole, Papierkorb, Menü) können meist unter Verwendung eines Zeigegerätes wie einer Maus gesteuert werden.
Ø 1970 erstes System mit GUI – Xerox Alto von Xerox PARC Ø 1983 Apple Lisa Ø 1985 Microsoft Windows 1.03 Ø 1992 Mircosoft Windows 3.1 Ø 2000/2002 KDE (QT) / GNOME (GTK) Ø 2009 Windows 7
Sommerkurs Python - 05. GUI TU Dresden, 23.05.2011 Folie 3 von XYZ
![Page 4: Übersichtknoll/python/material/SommerkursPython... · PyQT – Tutorial TU Dresden, 23.05.2011 Sommerkurs Python - 05. GUI Folie 12 von XYZ . Hello World >>> import sys](https://reader031.fdocuments.in/reader031/viewer/2022022601/5b48d9367f8b9a824f8cdcb2/html5/thumbnails/4.jpg)
Alternativen
Ø Tkinter Ø basiert auf Tcl/Tk Ø kein Plattformkonformes GUI
Ø wxPython Ø basiert auf wxWindows Ø Wrapper für native Bibliotheken (Win32 Controls, GTK)
Ø PyGTK Ø basiert auf GTK (Gimp Toolkit) Ø bedingt plattformunabhängig
Ø Pythonwin Ø Binding für Windows MFC Bibliothek (nicht portierbar)
Sommerkurs Python - 05. GUI TU Dresden, 23.05.2011 Folie 4 von XYZ
![Page 5: Übersichtknoll/python/material/SommerkursPython... · PyQT – Tutorial TU Dresden, 23.05.2011 Sommerkurs Python - 05. GUI Folie 12 von XYZ . Hello World >>> import sys](https://reader031.fdocuments.in/reader031/viewer/2022022601/5b48d9367f8b9a824f8cdcb2/html5/thumbnails/5.jpg)
Programmablauf
Sommerkurs Python - 05. GUI TU Dresden, 23.05.2011 Folie 5 von XYZ
![Page 6: Übersichtknoll/python/material/SommerkursPython... · PyQT – Tutorial TU Dresden, 23.05.2011 Sommerkurs Python - 05. GUI Folie 12 von XYZ . Hello World >>> import sys](https://reader031.fdocuments.in/reader031/viewer/2022022601/5b48d9367f8b9a824f8cdcb2/html5/thumbnails/6.jpg)
Widget
Ø Funktionales Element der GUI Ø Abgeschlossener Funktionsumfang Ø Graphische Repräsentation eines Models/Tätigkeit Ø Bsp.: Button, Textfeld, Tabelle, Label
Ø Achtung: Abgrenzung! Ø Hier explizit nicht Google, Dashboard oder Yahoo Widgets
Sommerkurs Python - 05. GUI TU Dresden, 23.05.2011 Folie 6 von XYZ
![Page 7: Übersichtknoll/python/material/SommerkursPython... · PyQT – Tutorial TU Dresden, 23.05.2011 Sommerkurs Python - 05. GUI Folie 12 von XYZ . Hello World >>> import sys](https://reader031.fdocuments.in/reader031/viewer/2022022601/5b48d9367f8b9a824f8cdcb2/html5/thumbnails/7.jpg)
Frame/Window
Ø Containerfläche zur Darstellung von Widgets
Ø Layout Management
Ø Ziel für Systembuttons (Schliessen, Maximieren, Minimieren)
Ø Titelzeile (mit Icon)
Sommerkurs Python - 05. GUI TU Dresden, 23.05.2011 Folie 7 von XYZ
![Page 8: Übersichtknoll/python/material/SommerkursPython... · PyQT – Tutorial TU Dresden, 23.05.2011 Sommerkurs Python - 05. GUI Folie 12 von XYZ . Hello World >>> import sys](https://reader031.fdocuments.in/reader031/viewer/2022022601/5b48d9367f8b9a824f8cdcb2/html5/thumbnails/8.jpg)
Dialog
Sommerkurs Python - 05. GUI
Ø Top Level Window
Ø Aufgaben bezogen
Ø Kommunikation mit Nutzer
Ø Modal vs. Non-Modal
Ø Rückgabewert und Standard Buttons Ø Ok, Cancel, Apply, Reset
TU Dresden, 23.05.2011 Folie 8 von XYZ
![Page 9: Übersichtknoll/python/material/SommerkursPython... · PyQT – Tutorial TU Dresden, 23.05.2011 Sommerkurs Python - 05. GUI Folie 12 von XYZ . Hello World >>> import sys](https://reader031.fdocuments.in/reader031/viewer/2022022601/5b48d9367f8b9a824f8cdcb2/html5/thumbnails/9.jpg)
MVC – Model View Controller
Sommerkurs Python - 05. GUI TU Dresden, 23.05.2011 Folie 9 von XYZ
![Page 10: Übersichtknoll/python/material/SommerkursPython... · PyQT – Tutorial TU Dresden, 23.05.2011 Sommerkurs Python - 05. GUI Folie 12 von XYZ . Hello World >>> import sys](https://reader031.fdocuments.in/reader031/viewer/2022022601/5b48d9367f8b9a824f8cdcb2/html5/thumbnails/10.jpg)
PYQT Python QT Binding
Sommerkurs Python - 05. GUI TU Dresden, 23.05.2011 Folie 10 von XYZ
![Page 11: Übersichtknoll/python/material/SommerkursPython... · PyQT – Tutorial TU Dresden, 23.05.2011 Sommerkurs Python - 05. GUI Folie 12 von XYZ . Hello World >>> import sys](https://reader031.fdocuments.in/reader031/viewer/2022022601/5b48d9367f8b9a824f8cdcb2/html5/thumbnails/11.jpg)
PyQT
Ø QT Ø aktuell Version 4.7 Ø entwickelt von Nokia, früher
Trolltech Ø liegt KDE zugrunde
Ø PyQT Ø Binding für QT
Ø Python(x,y) Ø QT – 4.5.2 Ø Python – 2.6
>>> import PyQt4
>>> help(PyQt4)
Sommerkurs Python - 05. GUI
QT Bibliothek
TU Dresden, 23.05.2011 Folie 11 von XYZ
![Page 12: Übersichtknoll/python/material/SommerkursPython... · PyQT – Tutorial TU Dresden, 23.05.2011 Sommerkurs Python - 05. GUI Folie 12 von XYZ . Hello World >>> import sys](https://reader031.fdocuments.in/reader031/viewer/2022022601/5b48d9367f8b9a824f8cdcb2/html5/thumbnails/12.jpg)
Nützliche Webseiten
Ø Riverbank http://www.riverbankcomputing.co.uk/software/pyqt/intro
Ø QT 4.5 - Referenz http://doc.qt.nokia.com/4.5/index.html
Ø PyQT – Tutorial http://zetcode.com/tutorials/pyqt4/
Sommerkurs Python - 05. GUI TU Dresden, 23.05.2011 Folie 12 von XYZ
![Page 13: Übersichtknoll/python/material/SommerkursPython... · PyQT – Tutorial TU Dresden, 23.05.2011 Sommerkurs Python - 05. GUI Folie 12 von XYZ . Hello World >>> import sys](https://reader031.fdocuments.in/reader031/viewer/2022022601/5b48d9367f8b9a824f8cdcb2/html5/thumbnails/13.jpg)
Hello World
>>> import sys >>> from PyQt4.QtCore import *
>>> from PyQt4.QtGui import *
>>> app = QApplication(sys.argv)
>>> widget = QWidget()
>>> widget.resize(250, 150)
>>> widget.setWindowTitle('Hello World')
>>> widget.show()
>>> sys.exit(app.exec_())
Sommerkurs Python - 05. GUI TU Dresden, 23.05.2011 Folie 13 von XYZ
![Page 14: Übersichtknoll/python/material/SommerkursPython... · PyQT – Tutorial TU Dresden, 23.05.2011 Sommerkurs Python - 05. GUI Folie 12 von XYZ . Hello World >>> import sys](https://reader031.fdocuments.in/reader031/viewer/2022022601/5b48d9367f8b9a824f8cdcb2/html5/thumbnails/14.jpg)
MyWidget
from PyQt4 import QtCore from PyQt4 import QtGui
from PyQt4.QtGui import QWidget, QIcon, QPushButton
class MyWidget(QWidget):
def __init__(self, parent = None):
QWidget.__init__(self, parent)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('MyWidget')
self.setWindowIcon(QIcon('icons/web.png'))
if __name__ == "__main__":
widget = MyWidget()
widget.show()
Sommerkurs Python - 05. GUI TU Dresden, 23.05.2011 Folie 14 von XYZ
![Page 15: Übersichtknoll/python/material/SommerkursPython... · PyQT – Tutorial TU Dresden, 23.05.2011 Sommerkurs Python - 05. GUI Folie 12 von XYZ . Hello World >>> import sys](https://reader031.fdocuments.in/reader031/viewer/2022022601/5b48d9367f8b9a824f8cdcb2/html5/thumbnails/15.jpg)
Signals and Slots
Ø Signale & Slots sind spezielle Funktionen
Ø Wird zur Laufzeit gekoppelt connect(Obj1, Fkt1, Obj2, Fkt2)
Ø Implementation des Observer-Pattern
Ø Alternative zu Callback Funktionen
Sommerkurs Python - 05. GUI TU Dresden, 23.05.2011 Folie 15 von XYZ
![Page 16: Übersichtknoll/python/material/SommerkursPython... · PyQT – Tutorial TU Dresden, 23.05.2011 Sommerkurs Python - 05. GUI Folie 12 von XYZ . Hello World >>> import sys](https://reader031.fdocuments.in/reader031/viewer/2022022601/5b48d9367f8b9a824f8cdcb2/html5/thumbnails/16.jpg)
Signals & Slots - Beispiel
from PyQt4 import QtCore from PyQt4 import QtGui
from PyQt4.QtGui import QWidget, QIcon, QPushButton
class MyWidget(QWidget):
def __init__(self, parent = None):
QWidget.__init__(self, parent)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('MyWidget')
self.setWindowIcon(QIcon('icons/web.png'))
quit = QPushButton('Close', self)
quit.setGeometry(10, 10, 60, 35)
self.connect( quit, QtCore.SIGNAL('clicked()'), QtGui.qApp, QtCore.SLOT('quit()') )
Sommerkurs Python - 05. GUI TU Dresden, 23.05.2011 Folie 16 von XYZ
![Page 17: Übersichtknoll/python/material/SommerkursPython... · PyQT – Tutorial TU Dresden, 23.05.2011 Sommerkurs Python - 05. GUI Folie 12 von XYZ . Hello World >>> import sys](https://reader031.fdocuments.in/reader031/viewer/2022022601/5b48d9367f8b9a824f8cdcb2/html5/thumbnails/17.jpg)
Composite
Sommerkurs Python - 05. GUI
Ø Design Pattern zur Abbildung von Hierarchien
Ø Rekursives Zusammenbauen von Inhalten
TU Dresden, 23.05.2011 Folie 17 von XYZ
![Page 18: Übersichtknoll/python/material/SommerkursPython... · PyQT – Tutorial TU Dresden, 23.05.2011 Sommerkurs Python - 05. GUI Folie 12 von XYZ . Hello World >>> import sys](https://reader031.fdocuments.in/reader031/viewer/2022022601/5b48d9367f8b9a824f8cdcb2/html5/thumbnails/18.jpg)
QT Designer
Sommerkurs Python - 05. GUI TU Dresden, 23.05.2011 Folie 18 von XYZ
![Page 19: Übersichtknoll/python/material/SommerkursPython... · PyQT – Tutorial TU Dresden, 23.05.2011 Sommerkurs Python - 05. GUI Folie 12 von XYZ . Hello World >>> import sys](https://reader031.fdocuments.in/reader031/viewer/2022022601/5b48d9367f8b9a824f8cdcb2/html5/thumbnails/19.jpg)
QT Designer
Ø Visuelles Erstellen von UIs
Ø Generiert *.ui Dateien basierend auf QML Ø XML basierte Beschreibungssprache
Ø Widgetnamen Ø können über Object Inspector eingestellt werden Ø können für Programmierung genutzt werden
Ø pyuic4 erstellt Python Datei aus *.ui Datei C:\Python26\Lib\site-packages\PyQt4\pyuic4.bat –o <OutputDatei> <InputDatei>
Sommerkurs Python - 05. GUI TU Dresden, 23.05.2011 Folie 19 von XYZ
![Page 20: Übersichtknoll/python/material/SommerkursPython... · PyQT – Tutorial TU Dresden, 23.05.2011 Sommerkurs Python - 05. GUI Folie 12 von XYZ . Hello World >>> import sys](https://reader031.fdocuments.in/reader031/viewer/2022022601/5b48d9367f8b9a824f8cdcb2/html5/thumbnails/20.jpg)
QTDesigner - Beispiel
Ø Ui_MainWindow.py Ø erzeugt über pyuic4 aus DuplicateFinder.ui
Ø In eigenes Projekt einfügen
from PyQt4.QtGui import QMainWindow from Ui_MainWindow import Ui_MainWindow
class DuplicateFinderUI(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.lineEdit.setText('C:\\Users\\')
TU Dresden, 23.05.2011 Sommerkurs Python - 05. GUI Folie 20 von XYZ
![Page 21: Übersichtknoll/python/material/SommerkursPython... · PyQT – Tutorial TU Dresden, 23.05.2011 Sommerkurs Python - 05. GUI Folie 12 von XYZ . Hello World >>> import sys](https://reader031.fdocuments.in/reader031/viewer/2022022601/5b48d9367f8b9a824f8cdcb2/html5/thumbnails/21.jpg)
Threads
Ø Thread Ø einzelner Programmablauf Ø unabhängig von anderen Threads (Nebenläufigkeit) Ø Hauptprogramm -> "Thread-0"
Ø Hauptklasse QThread (PyQt4.QtCore) Ø einfache Kommunikation über SIGNALs & SLOTs Ø 3 Phasen
Ø Initialisierung (__init__ Method) Ø Start (start Methode) – ist gegeben, muss nicht programmiert werden Ø Laufzeit (run Methode)
Ø Alle Threads werden beendet, wenn "Thread-0" beendet wird!
TU Dresden, 23.05.2011 Sommerkurs Python - 05. GUI Folie 21 von XYZ
![Page 22: Übersichtknoll/python/material/SommerkursPython... · PyQT – Tutorial TU Dresden, 23.05.2011 Sommerkurs Python - 05. GUI Folie 12 von XYZ . Hello World >>> import sys](https://reader031.fdocuments.in/reader031/viewer/2022022601/5b48d9367f8b9a824f8cdcb2/html5/thumbnails/22.jpg)
Standard Dialoge
Operation Erklärung QColorDialog Farbwähler QErrorMessage Fehlermeldung QFileDialog Datei- oder Verzeichnisauswahl QFontDialog Schriftartwahl QInputDialog Einfacher Dialog um einen Wert abzufragen QMessageBox Modaler Dialog zur Informationsdarstellung QPrintDialog Druckdialog QProgressDialog Zustandsanzeige QDialog Allgemeiner Dialog - Basisklasse aller Dialoge
Sommerkurs Python - 05. GUI TU Dresden, 23.05.2011 Folie 22 von XYZ