Python + Eric + pyqt installation configuration and the first program helloworld

Source: Internet
Author: User

Today, we are going to start a network marketing tool. We need to use Python + QT for development. For the first use, the following describes the installation and configuration steps and initial development details. For each type of development, we will write a helloworld program at the beginning. After the environment configuration is provided, I will explain how to write the first program helloworld.

1. Installation instructions

(1) download and install Python

First download the python: http://www.python.org/ftp/python. There are various versions available for you to download as required.

The download I selected is: http://www.python.org/ftp/python/3.2.2/python-3.2.2.msi

After installation, you must configure the environment variable and add the python directory to the environment variable path. For example, if my python installation directory is "D: \ python27", I add "D: \ python27" to the environment variable path.

(2) download and install pyqt

: Http://www.riverbankcomputing.co.uk/software/pyqt/download

Press enter to install the SDK. It is installed in the python directory by default. For example, I installed it in: D: \ python27.

(3) download and install Eric

For: http://eric-ide.python-projects.org/eric-download.html

Eric4 is a python and Ruby ide with powerful code functions and perfect combination with qt4. This makes it easy to develop Python applications with graphical interfaces. Eric4 is written in pyqt. Therefore, Python must be installed before installation. After decompression, double-click Install. py. After the installation is complete, click eric4.bat to run. After the installation is complete for the first time, you need to configure it. If your software is not running for the first time, there is no configure named pythonw (configuration dialog box) popped up, you can click the eric4-configure.bat to bring up.

The configuration process is as follows:

Click editor -- autocompation --- to check all the check boxes. Qscintilla --- check the two selection boxes left and right, and then select from document and API files in the source below.
Click editor ---- APIs ---- to hook up complie APIs autocompation, and then select python in language. Click the add from installed APIs button below and select the. API file. Click compile APIs.
The installation process is complete.

2. The first helloworld Program

(1) run the D: \ python27 \ eric4.bat Startup Program, or click D: \ python27 \ Lib \ Site-packages \ eric4 \ eric4.py to start the program. Click project --- new in the menu.
Projcet name: helloworld
Projcet type: qt4 Gui
Projcet Directory: select the project file directory you plan to store.
Click OK. The version selection dialog box is displayed. Select none.
(2) Click the second tab forms in projcet-Viewer on the left of the software interface.
In the blank area below, right click -- new form... in the pop-up dialog box, select dialog, and then OK. Name the UI file (helloworld. UI). After saving, the qt4 design window will pop up. Select line edit to draw a single row text box, and change objcetname to ltext in the right attribute box, you can easily remember ).
Then draw a button, change its name to btn1, and change text to hello.
Add a button in the drawing, change its name to btn2, and change text to exit.
(3) design the signal and slot for the exit button. When you click it, it will exit.
Click the plus sign (+) in the (signal/slot Editor) on the right of the window to display an undefined event, in this case, select btn2 as the sender (btn2 is the exit button just defined), clicked () as the signal, dialog as the receiver, and close () as the slot (), save and close the designer.

The following describes the concepts of "signal" and "slot": In pyqt4, the terms of 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. Here, the "exit" button sends an event signal. The form dialog is the receiver of the event, that is, the slot, and the form processes the event signal.
(4) After returning to the Eric interface, a file named helloworld. UI appears on the forms tab on the left and right. Right-click the file name, select complie form, and click OK.
Click the source tab under projcet-viewer and you will see a file named ui_helloworld.py. Double-click the file and open-source file to view its content. A ui_helloworld class is generated. Ui_helloworld.py can be run independently. Press the F2 key. After the execution, the program we just designed will appear. This Python program has been developed. The Hello button does not respond because it is not encoded yet, but the program exits after you click the exit button.

Note: It is not recommended to manually modify ui_dlghello.py, because every time you finish modifying the interface and complie form, you will overwrite the previous manual modification.
(5) encode the hello button. After you click it, the text of Hello world is displayed in the text box.
In pyqt4, the interface code is separated from the event code, so that every time you change the interface, the code for event processing will not be affected. To create a new class for event processing, write the event processing function in the new class. You can create a class in eric4.

The specific process is as follows: Right-click helloworld. UI, select generate dialog Code, set classname, and click the new button on the right. By default. After confirming. In the text box below, you can select the event we are interested in. eric4 will generate the definition of the event processing function together. Here we select btn1 to check the first on_btn1_clicked () and then OK. Click the first tab source under projcet-viewer, and an additional helloworld. py file will be displayed. Double-click to open the file. A script generated for the btn1 button will appear after the file.

@pyqtSignature("")def on_btn1_clicked(self):        """       Slot documentation goes here.       """        # TODO: not implemented yet        raise NotImplementedError  

The following describes the script:

Note that this @ pyqtsignature ("") automatically processes the association between the slot (event processing function) defined below and the corresponding signal (event. Click the btn1 button to automatically execute this function. The naming rule for slot is"On _ OBJECT name_signal name", If you want to add a new slot, just add the function according to this rule, and add @ pyqtsignature (" ") before the Function Definition Statement, no need to regenerate helloworld. 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.

OK. Continue. We need to modify the code snippet above:

@pyqtSignature("")def on_btn1_clicked(self):        self.LText.setText("Hello World")   

Add the Python code to the top of the source file.

import PyQt4, PyQt4.QtGui, sys  

Add Python code at the bottom

if __name__ == "__main__":        app = PyQt4.QtGui.QApplication(sys.argv)       dlg = Dialog()       dlg.show()       sys.exit(app.exec_())  

The first Python program has been designed. Press F5 to run.

Note: If the button text is Chinese characters, the button text will be garbled after running. For example, if we set the text of btn1 to "hello", the text of the btn1 button is garbled after running.

Solution 1: Change ("hello") in the code corresponding to btn1 to (u "Hello, pyqt4. However, after this change, the previous changes will be lost after the interface is re-compiled.

In this case, we can use method 2 (recommended ), change the "Python" encoding in "Preferences" in "Settings" in the Eric menu to "UTF-8.

Pyqt4 provides excellent support for Chinese characters. The Code uses UTF-8 encoding in a unified manner, saving a lot of trouble.

Bytes ---------------------------------------------------------------------------------------------------------------------------

Note: The final helloworld. py file is as follows:

# -*- coding: utf-8 -*-"""Module implementing Dialog."""from PyQt4.QtGui import QDialogfrom PyQt4.QtCore import pyqtSignaturefrom Ui_HelloWorld import Ui_Dialogimport PyQt4, PyQt4.QtGui, sysclass Dialog(QDialog, Ui_Dialog):    """    Class documentation goes here.    """    def __init__(self, parent = None):        """        Constructor        """        QDialog.__init__(self, parent)        self.setupUi(self)        @pyqtSignature("")    def on_btn1_clicked(self):        """        Slot documentation goes here.        """        # TODO: not implemented yet        #raise NotImplementedError        self.LText.setText("Hello World")              if __name__ == '__main__':       app = PyQt4.QtGui.QApplication(sys.argv)       dlg = Dialog()       dlg.show()       sys.exit(app.exec_())

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.