QML and PyQT: Creating a GUI (tutorial)

Standard

If you have not done our first PyQT tutorial yet, you should do it, it’s fun! In this tutorial we will use an user interface markup language, a language that describes the graphical  user interfaces and controls .  An excerpt of user interface markup language code could look like:

Essentially the language tells what the interface should look like. The language we will be using is called QML. Do not worry about it for now.

Start a programmed called QTCreator.   The tutorial will be quite graphical to help you through the whole process. Simply type qtcreator in the terminal or start it from the menu list.  This screen should pop up:

qtcreator

Press the big New Project button. Select QT Quick Application from the menu below. Finally press Choose on the bottom right.

QT Quick Project QML

A new popup will appear:

kde create

Type a name and a valid path to save your project. Then press Next.  Select QT Quick 2.0 from the menu list. Press Next. Press Finish. Immediately a user interface defined in the QML language will appear.

qtquick2

Like all great programmers we are going to solve things the most lazy way possible.  Instead of typing all the QML code by hand we are going to press the Design button that is on the left of the screen.  A drag and drop screen should appear now.

QT draganddrop

We drag an image onto the area and select the source on the right. Save the project. Open a terminal and locate the qml file you just created. Alternatively you could simply copy the code in the edit box and save it to a .qml file. Enter the command:

This will display the windows as defined in the qml file without any functionality. It is simply a viewer for the interface.  We then create some code to load this QML definition:

Finally we modify the first line main.qml to:

Simply because our QtQuick was missing. Running

Will now display our user interface as defined by QML:

QML_PyQT

All of the code is simply PyQT, so you could add code like in the previous tutorial. These are the two methods to create a graphical interface with PyQT.  This method may be more loosely coupled to the code compared to the method of creating a GUI with QT in the previous tutorial. Despite that both are valid methods.

Building an application GUI with PyQT, beginners tutorial

Standard

In this tutorial we will teach you how to create a graphical application with PyQT.  You will need to install some packages:

If python-kde4 cannot be found update your repository to find it. If you are on Ubuntu use this link.

Now we can use the QT Designer application. It saves us from writing tons of layout code that you may be used to when writing HTML.  Start qt4-designer from your applications menu. The QT Designer application will appear:

QT_Designer

QT_Designer

Press Dialog without Buttons and press Create. You can now drag any component from the widget box to the form. Simple drag and drop. We added a button, label and a pixmap.  (I took a random image from the web for the pixmap)

QT_KDE_Dialog

Our window looks like the image above. Press Form > Viewcode. We will get a popup box with the form code in… C++! That is great, but we want the Python code.   Press File > Save as > form.ui.

The file test.ui contains your form described in XML format. (You can view it in a text editor) Open a console and type:

Running the file does nothing. Create a new file called  gui.py

Paste the code below:

Run with:

This will open our graphical interface. Pressing on the OK button will simply close the application.

PyQT window

We want to add some action when the OK button is pressed.  We add these three lines to the code:

Please make sure your button is named pushButton by inspecting the form.py file. Pressing the button will print ‘OK pressed’ to the terminal.

pyqt_window2

Total application below:

gui.py

form.py (generated):

 

Creating a gui in Python with WxWidgets [tutorial for beginners]

Standard

Python programming is a lot of fun and sometimes we want to create a graphical application (GUI). WxWidgets can be used to create a graphical interface that looks like a native application on any operating system. First download and install WxPython, the Python bindings for wxWidgets.

Then install a GUI creator called wxglade:

Using a GUI builder such as wxGlade will save you a lot of time, regardless of the GUI library you use. You can easily make complex graphical interfaces because you can simply drag and drop.

Creating our first GUI with Python and wxWidgets:

Start wxglade. You will see its user interface:

wxglade

Press on tiny window on the top left, below the file icon.

wxglade frame

Press OK.  An empty window will now appear.  Press on the tiny [OK] button in the wxGlade panel and press on the frame. The button will now appear. Press on Application in the tree window.

wxglade2

Set the output file in the wxproperties window.

wxglade3

If you look at the window note you can select multiple programming languages and two versions of wxWidgets. Select Python and wxWidgets 2.8.  Finally press Generate code. (Do NOT name the file wx.py because the import needs wx, save it as window.py or something else).

Putting it all together:
Run:

And a window with a button will appear. Pressing the button will not do anything. To start a function when pressing the button, we need to define a so called Callback. This can be as simple as:

Finally we bind the button to the callback function using:

Pressing the button will now write a message to the command line. Instead of the boring command line message, we want to show a message box. This can be done using this command:

The full code below:

wxWidgets gui python