当前位置:网站首页>(2) Pyqt5 tutorial -- > using qtdesigner to separate interface code
(2) Pyqt5 tutorial -- > using qtdesigner to separate interface code
2022-06-25 12:36:00 【haoming Hu】
GitHub Connect :
This column all source code GitHub Through train
I've talked about one in the last article pyqt5 What is the basic framework of , This article uses QTdesigner Let's design the interface
Chapter one :pyqt5 Basic framework
1、QTdesigner
QTdesigner It's a GUI Visual design software , I wasn't there Python the pyqt5 tools Inside down , I did it by installing QT5.9.8 To get , Because you may use it later C++ Written QT. This visualization tool is actually similar to visual base、C# In this language , These two languages can be used to design the upper computer directly by dragging GUI Interface design , And then generate the code , I just need to write the logic code of the interface .
start-up QTdesigner Choose the last one , You can create a blank window 
The left column is all kinds of controls , Here I pull two text edit、 Two pushbutton Come out and form an interface 
Save it at this time .
The saved file is .ui file , It's standard xml file , And in the development process , If it is to use C++ Development needs to be transformed into .cpp file , Now it is pyqt, So we need to .ui File to .py file , stay .ui File directory command line input command
pyuic5 -o demo.py demo.ui
Take a look at my design above ui File corresponding py Code
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '02.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(748, 523)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(90, 160, 75, 23))
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"))
2、 Call the designed interface code
At the beginning, we will talk about the separation of interface and code .
First of all, we need two at this time py file , One is just from ui It's a document py file , One is that our program runs py file , In other words, the generated code file should be used as a package . In the new py File to import this package
therefore import Part of it is :
from ui2 import Ui_Form# Import on QTdesigner Well designed ui.py file
import sys
from PyQt5 import QtWidgets
The main program frame remains unchanged :
mydesigner It's still a class defined by myself
QApplication amount to main function , That's the whole program ( There are a lot of documents ) Main entry function of .
For one Gui The program must have at least one such instance to run .
The last sentence is to call sys Library exit Exit method , Exit conditions ( Parameters ) yes
app.exec_() That is, the whole window is closed .
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
myshow = mydesigner()
myshow.show()
sys.exit(app.exec_())
Custom class :
class mydesigner(QtWidgets.QWidget):
def __init__(self):
super(mydesigner,self).__init__()
self.new = Ui_Form()# Create examples
self.new.setupUi(self)# Load form , But not yet
You can also make mydesigner This class inherits custom Ui_Form class , At this point, you can directly self.setupUi,
The difference between inheriting and not inheriting is calling Ui_Form The contents and methods of classes are different .Ui_Form Class is to use QTdesigner The generated class
class mydesigner(QtWidgets.QWidget,Ui_Form):
def __init__(self):
super(mydesigner,self).__init__()
self.setupUi(self)
Running results 
边栏推荐
- 2022 meisai e topic ideas sharing + translation
- ARM V7 协处理器
- Spicy food advertising e-commerce system development function and spicy food advertising e-commerce app system development source code sharing
- Possible causes of wechat applet decryption failure
- Array reorder based on a field
- 2022 Baidu collection batch automatic push assistant
- R language uses the scale function to scale the input data of neural network to the minimum and maximum, scale the data to between 0 and 1, and divide the data set into training set and test set
- High imitation blue playing network disk file sharing to make money network disk PHP system source code
- Node child processes and threads
- thinkphp3.2.5 GIF. class. php for php7.4
猜你喜欢

The first techo day Tencent technology open day in 2022 will be held online on June 28

Full nanny tutorial of Market Research Competition (experience sharing)

Uncover gaussdb (for redis): comprehensive comparison of CODIS

Service charge and time setting code sharing involved in crmeb withdrawal process

Windows下MySQL的安装和删除
![[on]learning dynamic and hierarchical traffic spatiotemporal features with transformer](/img/58/d68112a3d019de66150e2f5102f436.png)
[on]learning dynamic and hierarchical traffic spatiotemporal features with transformer

Zhangxiaobai's road to penetration (7) -sql injection detailed operation steps -union joint query injection

Zhangxiaobai's way of penetration (V) -- detailed explanation of upload vulnerability and parsing vulnerability

Total number of MySQL statistics, used and unused

15. Notes on the button style of WPF
随机推荐
PHP files running online
Arm V7 coprocessor
Jeecgboot startup popup configuration is still incorrect
揭秘GaussDB(for Redis):全面對比Codis
Rank sum ratio comprehensive evaluation method for common models in mathematical modeling
How to use ARIMA model for prediction?
Heyangdao store management system -- share the development source code of heyangdao system
ARM V7 连续加载/存储
Is it safe to open an account and buy stocks on the Internet?
JQ dynamic setting radio does not take effect when selected
20. MVVM command binding of WPF
[regression analysis] understand ridge regression with case teaching
Installation and removal of MySQL under Windows
[oceanbase] Introduction to oceanbase and its comparison with MySQL
Mpai data science platform random forest classification \ explanation of regression parameter adjustment
High imitation blue playing network disk file sharing to make money network disk PHP system source code
ECSHOP commodity wholesale multi attribute multi specification multi inventory batch purchase ECSHOP wholesale plug-in ECSHOP multi attribute order
Set the transparency of the picture to gradient from left to right
The server reported an error 503 service unavailable:the system returned: (71) protocol error
Zhengzheng e-commerce source code -- Zhengzheng advertising e-commerce system development source code sharing