PyQt5 Creating the first form (regular routines)

Source: Internet
Author: User

First, Pyqt5 Create a first form

Many people write form programs are directly knocking code, do not use the designer, I personally do not very much agree with this practice. The benefits of using the designer are intuitive, easy to maintain, and especially efficient for developing complex forms.

However, each time the UI file is modified, the Py file needs to be rebuilt, overwriting the original content, and in order to avoid this, an intermediate class is introduced. The main steps are as follows:

1 Create a simple dialog box with Qtdesigner and save it as a simpledialog.ui.

2 use CMD to enter the directory where the Simpledialog.ui file is located, run the command pyuic5 simpledialog.ui >ui_simpledialog.py

3 Open the UI_SIMPLEDIALOG.PY,PYQT automatically generated code as follows (no modification required):

From PYQT5 import Qtcore, Qtgui, qtwidgets

Class Ui_form (object):

def setupui (self, Form):

Form.setobjectname ("Form")

Form.resize (400, 300)

Self.pushbutton = Qtwidgets.qpushbutton (Form)

Self.pushButton.setGeometry (Qtcore.qrect (60, 70, 241, 131))

Self.pushButton.setObjectName ("Pushbutton")

Self.retranslateui (Form)

Self.pushButton.clicked.connect (Form.close)

QtCore.QMetaObject.connectSlotsByName (Form)

def retranslateui (self, Form):

_translate = QtCore.QCoreApplication.translate

Form.setwindowtitle (_translate ("form", "form"))

Self.pushButton.setText (_translate ("Form", "pushbutton"))

You can see that the class name of the dialog box you just created is "Ui_form".

4 Create a new simpledialog.py file and write the following code:

From PYQT5 import Qtcore, Qtgui, qtwidgets

From Ui_simpledialog import Ui_form

Import Sys

Class Simpledialogform (ui_form): #从自动生成的界面类继承

def __init__ (self, parent = None):

Super (Simpledialogform, self). __init__ ()

def yourfunctions (self):

Pass

#这个类中处理你自己的业务逻辑. Interface UI can be arbitrarily modified without affecting the logic you have written.

Customizing a Class (Simpledialogform) inherits from the PYQT auto-generated class, then overwrite the constructor, and the remaining work is what you want to add.

5 Perfect run the test code as follows:

if __name__ = = "__main__":

App = Qtwidgets.qapplication (SYS.ARGV)

main = Qtwidgets.qmainwindow () #创建一个主窗体 (must have a main form)

Content = Simpledialogform () #创建对话框

CONTENT.SETUPUI (Main) #将对话框依附于主窗体

Main.show () #主窗体显示

Sys.exit (App.exec_ ())

6 Why do you do this?

Based on the principle of interface and implementation separation, we use qtdesigner design form, just as the interface, the specific business logic through a separate file implementation, so that if the interface changes, PYQT automatically generate the form code will not overwrite the business logic we have written.

PyQt5 Creating the first form (regular routines)

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.