[Post] compile a python GUI program with pyqt4 in eric4

Source: Internet
Author: User

This document is an introductory tutorial on pyqt4. I can find other tutorials on the Internet, but I don't think it is very clear. I hope this article will be more helpful to the readers.

First, we will introduce pyqt4. The qt4 graphics library has been well received since it was released. Its binding to pyqt4 in Python makes me shine, not only beautiful, but also very convenient to develop programs.

In my opinion, one of the biggest improvements of pyqt4 is that it is no longer stuck with various layout controls. That is to say, write the graphic interface program now, like VB, you can drag controls directly to the window and change the size and position at will. Instead of placing layout controls first, you can place other controls in the layout control.

The introduction to pyqt4 is limited here, and I am not going to compare it with other Python graphics libraries, because experience shows the comparison of these things, in particular, the comparison between QT and GTK always leads to unnecessary quarrels.

Ide I use eric4. Eric4 is written in pyqt4. When using eric4, you can see how many graphic interfaces can be written in pyqt4. For more information about how to install eric4, see my other articles.

The operating system is windows, and the operations are the same in Linux.

The cold answer is over. Suppose we want to use python to write a graphical interface program, a dialog box with two buttons and one label. Click a button to change the label content. Click another button to exit.

1. Create a project.

Open eric4 and choose Project> New to create a new project. Set the name to hellopyqt, fill in the items, and click OK after selecting the folder where the project is located, A new project without any files is created.

2. Create dialog box.

Switch to the forms tab in projectviewer on the left (the second one on the left), right-click the blank position, select new form, and select the form type as dialog in the pop-up dialog box, then you will be asked where to save it. I set it to save as the dlghello. UI file. Click OK to create the file and open qtdesigner automatically.

3. Design the interface.

First, modify the attributes of the Main Dialog Box. Select the dialog box to view/modify the properties of the dialog box in the property editor on the right. Change windowtitle to "Hello, pyqt" and objectname to "dlghello". The former is the title of the dialog box, and the latter is useful when code is generated later. The default value is not recommended.

Drag a label (in displaywidgets classification) to the dialog box, change the attribute text to "Hello, pyqt", and change objectname to lblhello.

Drag two Pushbutton (in the buttons category) to the dialog box and change the attribute text to "hello" and "exit" respectively ". Change the attribute objectname to btnhello and btnexit.

The interface looks like this:

4. Process events.

In pyqt4, the terms for event processing are "signal" and "slot", namely, signal and slot. The event corresponds to the signal, and the event processing function is the slot.

Pyqt4 has some predefined slots that we can use directly. For example, the slot of the "exit" button is actually to close the dialog box. This slot has been defined in pyqt4. For such a slot, we do not need to write code separately, it can be completed in qtdesigner. For the "hello" button, we need to write the code by ourselves. For such slots, we do not process or even define them in qtdesigner.

In this example, in qtdesigner, we only process the Click Event of the exit button.

Click "Edit signal/slot" to enter the signal/slot editing mode. Click the exit button and drag it to display something like the grounding icon in the circuit diagram, as shown below:

 

With the mouse removed, the "Configure connection" dialog box is displayed. Check "display signals and slots inherited from qwidget", select clicked () on the left, close () on the Right, and click OK.

 

To continue to adjust the dialog box, click "Edit widget" to return to the window editing mode.

5. Generate the interface code

Close qtdesigner and you will find that dlghello. UI is displayed in the forms tab of eric4 projectviewer. Right-click it and select compile form to generate the ui_dlghello.py file and add it to the project automatically. You can see on the sources tab.

Double-click ui_dlghello.py to view its content. In fact, a ui_dlghello class is generated. Ui_dlghello.py can be run independently. In eric4, press f2 to run it directly to see the initial results. You can click the exit button to exit the program.

We do not recommend that you manually modify ui_dlghello.py because the manual modification will be overwritten after the interface is changed and the code is generated.

6. Add additional code.

Click the "hello" button to process the code.

In pyqt4, the interface code is separated from the event code, which is very good, so that every time you change the interface, the event processing code will not be affected. Wxpython is not doing well.

To create a new class for event processing and inherit from the dlghello class, write the event processing function in the new class. You can create a class in eric4. Right-click dlghello. ui: Select generate dialog code. In the displayed dialog box, set classname to dlghello. In the dialog box, you can select the event you are interested in, eric4 will generate the definition of the event processing function together. For example:

Click OK, and then dlghello. py is generated. Open this file. The "hello" button event is defined:

@ Pyqtsignature ("")

Def on_btnhello_clicked (Self ):

"""

Slot documentation goes here.

"""

# Todo: not implemented yet

Raise notimplementederror

Note that this @ pyqtsignature ("") automatically processes the association between the slot (event processing function) defined below and the corresponding signal (Event). Here, click the btnhello button to automatically execute this function. The naming rule of the slot is "on _ peer image name_signal name". To add a new slot, just add the function according to this rule, add @ pyqtsignature ("") before the Function Definition Statement, and you do not need to generate a new dlghello. py file.

In fact, another method to associate signal with slot is to bind it at runtime. For example, the channel for the clicked signal of the button object btnabout is the about_clicked function, add the following sentence to the _ init _ function:

Pyqt4.qtcore. qobject. Connect (self. btnabout, pyqt4.qtcore. Signal ("clicked ()"), self. about_clicked)

Then, the about_clicked function is executed when the btnabout button is clicked.

The two methods have their own advantages. The first method is simple, and the second method is effective when multiple signal uses the same slot.

Change the on_btnhello_clicked function:

@ Pyqtsignature ("")

Def on_btnhello_clicked (Self ):

Self. lblhello. settext ("Hello, pyqt4 ")

Add the following to the file header:

Import pyqt4, pyqt4.qtgui, sys

Add at the end of the Code (almost the same as that at the end of ui_dlghello.py ):

If _ name _ = "_ main __":

APP = pyqt4.qtgui. qapplication (SYS. argv)

DLG = dlghello ()

DLG. Show ()

Sys.exit(app.exe C _())

In this case, OK.

 

7. Final work.

Press f2 to run the script and find that the text in lblhello label is garbled after you click "hello.

The solution is simple. Just change ("Hello, pyqt4") in the code to (u "Hello, pyqt4. Pyqt4 provides excellent support for Chinese characters. The Code uses UTF-8 encoding in a unified manner, saving a lot of trouble.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.