Introduction to the PYQT5 GUI Application Toolkit (2)

Source: Internet
Author: User
Tags qt designer


Let's look at the PYQT5 development process from a simple login dialog box.
PYQT5 supports two types of interface development: one is to design the interface in the Qtdesigner, the various controls are added directly by dragging, the other is to write the interface code directly, through the control of the interface by code. Below we will create a login dialog box, shown in Figure 1, respectively, in both of these ways.


Figure 11 A Simple Login dialog box Qtdesigner design interface

After the setting of the previous article, we can open the Qtdesigner program through the menu bar "Tool" → "qt Tool" → "qt Designer", as shown in Figure 2.


Figure 2 Qtdesinger interface

Since we only need to create a simple window, we select the template as "Widget" and click "Create" to enter the window editing interface, as shown in Figure 3.

Figure 3 New Window

All we have to do is drag the controls we need into the 2 push Button (buttons), 2 label (Display widget) and 2 line Edit (Import widgets). The position is roughly arranged when dragged in (Figure 4).

Figure 4 Window Preliminary design

Click the right mouse button in the margin of the window and choose layout → Grid layout to arrange the controls roughly.

Then, by clicking the object in the Object Viewer, you can set the control properties in the Property Editing dialog box. In this project we make the following settings:

Set the form's "WindowTitle" as the "Login Interface",

Set two label "text" to "username" and "password", respectively.

Set the two button "text" to be determined and canceled, respectively. When you are finished setting, adjust the window to the appropriate size (Figure 5).


Figure 5 Final window

Finally, save this window as a UI file in the project folder, select this file, click the right mouse button, select "QT Tool" → "Pyuic", you can generate a py file.
Directly compose the interface code

The UI files generated in the previous section need to be converted into a py file for use. Therefore, we can also directly write the interface code, to develop.
The code is as follows:

#-*-Coding:utf-8-*-

From pyqt5.qtwidgets Import *

Class Ui_form (Qwidget):

def __init__ (self):

Super (). __INIT__ ()

Self.setwindowtitle ("Login Interface")

Self.resize (200,100)

Lay=qformlayout ()

Self.setlayout (Lay)

Lab1=qlabel ("User name")

Line1=qlineedit ()

LAB2 = Qlabel ("password")

Line2 = Qlineedit ()

Okb=qpushbutton ("OK")

Cb=qpushbutton ("Cancel")

Lay.addrow (LAB1,LINE1)

Lay.addrow (LAB2, Line2)

Lay.addrow (OKB,CB)

Code Description:

Line 2nd, import PyQt5 Control, PyQt5 of the various controls are generally placed in the Pyqt5.qtwidgets module, the bank code imported all the controls, you can only import the control used by this program, that is, writing "from Pyqt5.qtwidgets Import Qwidget,qformlayout,qlabel,qlineedit,qpushbutton ", people familiar with the Python grammar may have other writing, this article no longer repeat;

Line 4th, establish Ui_form class, derive from Qwidget base class;

The 5th, 6 lines, the constructor, the complete writing is:

def init (Self,parent=none):
Super (Ui_form,self). Init (parent)
But in the Python3 can be simplified as the 5th, 6 lines of the form;

Line 7th, set the window name;

Line 8th, set the window size;

9th, 10 lines, set the overall layout;

Line 12th to 19th, set up the control object;

Line 21st to 3rd, add the control to the layout. Running programs

The above generated window class can be invoked by writing the main function in the same PY file. But I do not recommend doing so. In particular, when you need to modify a window in the first method, you also want to regenerate the py file, and you need to override the main function.
We can write another main.py to run the dialog box. The code is as follows:

#-*-Coding:utf-8-*-

From PYQT5 import qtwidgets

From DLG import Ui_form

Import Sys

if __name__ = = "__main__":

App = Qtwidgets.qapplication (SYS.ARGV)

Myshow = Ui_form ()

Myshow.show ()

Sys.exit (App.exec_ ())

Code Description:

Line 3rd, import the class from the dialog file, Dlg to the dialog box file name;

Line 6th, the main program is defined under this line statement, enter "main" and press "tab" key to quickly enter;

Run main.py, you can run this dialog box program.

Commend the author


The author of this article

Stream

Master of Civil Engineering, the master period using Python data processing and abaqus two development, is now working in a civil engineering consulting company in Shanghai, engaged in civil engineering software development.

Click to read the original, collect 100 yuan voucher of the reptile course, limit 50 names

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.