>> from qt import * ● We are set for this tutorial. ● Assumptions made – Some knowledge of program control structures – Some understanding of python Syntax"> >> from qt import * ● We are set for this tutorial. ● Assumptions made – Some knowledge of program control structures – Some understanding of python Syntax">
Download presentation
Presentation is loading. Please wait.
Published byDylan Underwood Modified over 8 years ago
1
Programming Tutorial using PyQT George Patterson Relating to {Flightgear presentation given at Barcamp Melbourne 2.0} Requires: ● qtDesigner 3.X ● Pyuic (user interface compiler) ● pyqt 3.X, python 2.4/2.5
2
About Me ● System Engineer – Supporting Approx 450 Debian servers – Flightgear Armchair pilot ● Trust me it's safer. ● Previous Experience – Network Administration – Web Programming All code included in this tutorial or the accompanying tar archve is licenses under GPL 2 license
3
Installation check ● Programming Tutorial using PyQT /home/user> python Python 2.4.4 (#2, Apr 5 2007, 20:11:18) [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from qt import * ● We are set for this tutorial. ● Assumptions made – Some knowledge of program control structures – Some understanding of python Syntax
4
Coding the Interface ● There are two different main ways to program a GUI interface ● The first is to hand code – Slow to prototype and modify afterwards – Difficult to visualise the output – Totally portable, requiring just a text editor ● GUI Design Tools – Quick prototyping and modifcation is easy – Interactive design process – Uses tools that may not be available in the field
5
Coding the Interface: Hand 1 ● Hello world in seven lines of code #!/usr/bin/env python import sys from qt import * a = QApplication(sys.argv) hello = QLabel("Hello world!",None) a.setMainWidget(hello) hello.show() a.exec_loop()
6
Hello World ● The interface is simply a button which has the words Helo World displayed – While simple it demonstrates the layers of software required. See hello/world.py for code ● Modules are load containing classes which do stuff. ● Slots bind an event to a method or function. – Eg. print a debug statement when a keypress occurs – Popup a dialog box stating “Don't not press the red button” when the user presses the button!
7
Coding the Interface: Hand 2 ● Introduction to widgets – The previous example did not use any user defined widgets ● Create a window containing one widget or button – When the button is pressied we print “Hello World” on the console. ● Simple and clean, though not helpful in a GUI world ● Challenges: Change the code to to display the detect “Button Pressed” on the button when the button is pressed. Could also change the text to “button Released” when thw user releases the button. Once the btton has been pressed, prevent the button from being pressed
8
I suppose... You have something better to do then? ● GUI Coding sucks and I have something better to do. Flying virtual planes for one. {Cue Monty Python's Meaning of Life sketch- Marching up and down the square}
9
Coding the Interface: Generated ● Introduction to qt Designer 3 The IDE approach of including an editor has been deprecated in QT4 in favour of plugins into existing IDEs. This means that there are multiple issues eg the code editor is removed, different ways to deal wth this. (Outside of scope of this tutorial) ● Ensure the GUI doesn't wait for anything – Has your appcation crashed? – Instead use a thread or separate process ● Threads can be hard to debug ● Separate process can be hard to track status
10
GUI Design ● Ensure a consistent look and feel with other applications. – Do not swap the Ok and Cancel buttons based on programmer perogative. ● Consider how the user will be using your application – allow for both keyboard and mouse ● Not every one has a 24” WS monitor. I don't. – Asus EeePC and MythTV are restricted in their screen resolution. Keep the target audience in mind
11
GUI Design: Subclasses ● Classes can be nested to extend the base class Your Application Overlaid methods and attributes PYUIC Prototype Class QT Schema Other Classes - Sockets - Hardware Ifs
12
GUI Development cycle ● Design the interface using qtDesigner ● Translate the design into a python class – Note: Do not modify this source file directly ● Extend the class with the application logic If you follow these steps, you will always be able to modify the interface without having to recode the code. (unless you start renaming attributes of the code)
13
Generated files ● Save you qtDesigner design as myform.ui – myform.h can be produced, we don't need it. ● run pyuic -o myform_ui.py myform.ui – myform_ui.py is produced. We no longer need myform.ui for our pthon program. ● So the files required to keep are:- – myform.ui (in case you need to mke changes) – myform_ui.py (can be regenerated at will) – myform.py (the python script to load the class) ● Makefiles are a good idea for larger projects. – Or “lazy” programmers
14
Thread Overview ● A treaded App will start another instance of itself. – Considered to be a lightweight process – the background “process” will run independently of the parent process. – Less memory usage than an true process. ● Many ways to do this – Threading module allows you to move data between processes. Great for results and setting flags
15
Outline of threads ● Outline of required code – import time, threading modules – Define your thread class class yourclass(threading.Thread): def __int__(self. param1, param2): threading.Thread__init(self) self.runthread=1 #and whatever else def classloop(self): while self.runhread=0: #do as you require ● And that's about it. – Might want to consider an escape plan
16
Writing your own Application ● Writing a text editor – plenty of room for improvement even without extending the builtin widgets – User preferences (sticky), MDI interfaces, font changes, collaboration editors. Interface to a wiki or blog. ● GUI interface to hardware – May require a library for the hardware – Requires a wrapper around the library for ease of programming. Consider abstracting the wrapper further ● Your App, Your Ruleset, Your Problem
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.