Pyside2 button, single line text box and text label of Python

Fantasy Lab 2022-05-14 14:47:42 阅读数:793

pyside2pysidebuttonsingleline

Interface effect

designer Drawing interface

If you haven't installed the corresponding library file , Please refer to the tutorial here :pyhton And GUI install PySide2、pyqt5-tools_ Fantasy lab blog -CSDN Blog install PyQt5( At the same time to install PyQt5 and Some important tools , such as Qt designer)https://blog.csdn.net/weixin_43775400/article/details/124763299?spm=1001.2014.3001.5501

use first :Qt designer Draw an interface ,

  Contains text labels (label), Single line text box (lineEdit), Button (pushButton

Here is PySide2 library , So after installation designer The starting path is :

Installation directory Scripts\pyside2-designer.exe

After the interface drawing is completed , preservation ui file ;

ui File conversion py Code

Use command , take ui file , convert to py Code

pyside2-uic main.ui > ui_main.py

notes : If you are in a virtual environment , take ui Put the files in the project directory of your virtual environment , then cd To the project catalog , Enter the conversion command ;

The code calls the preview interface

open ui_main.py file , We will see the following similar code

At this time, the interface effect cannot be previewed when running , Later, we will talk about how to call

from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
if not MainWindow.objectName():
MainWindow.setObjectName(u"MainWindow")
MainWindow.resize(508, 295)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName(u"centralwidget")
self.open_button = QPushButton(self.centralwidget)
self.open_button.setObjectName(u"open_button")
self.open_button.setGeometry(QRect(420, 30, 51, 21))
self.label = QLabel(self.centralwidget)
self.label.setObjectName(u"label")
self.label.setGeometry(QRect(50, 30, 31, 16))
self.lineEdit = QLineEdit(self.centralwidget)
self.lineEdit.setObjectName(u"lineEdit")
self.lineEdit.setGeometry(QRect(80, 30, 331, 20))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QMenuBar(MainWindow)
self.menubar.setObjectName(u"menubar")
self.menubar.setGeometry(QRect(0, 0, 508, 23))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QStatusBar(MainWindow)
self.statusbar.setObjectName(u"statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
# setupUi
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
self.open_button.setText(QCoreApplication.translate("MainWindow", u"\u6253\u5f00", None))
self.label.setText(QCoreApplication.translate("MainWindow", u"URL:", None))
self.lineEdit.setInputMask("")
# retranslateUi

At this point, we need to write the following code , Preview the interface call

if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

Complete code :

# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file 'main.ui'
##
## Created by: Qt User Interface Compiler version 5.15.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
if not MainWindow.objectName():
MainWindow.setObjectName(u"MainWindow")
MainWindow.resize(508, 295)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName(u"centralwidget")
self.open_button = QPushButton(self.centralwidget)
self.open_button.setObjectName(u"open_button")
self.open_button.setGeometry(QRect(420, 30, 51, 21))
self.label = QLabel(self.centralwidget)
self.label.setObjectName(u"label")
self.label.setGeometry(QRect(50, 30, 31, 16))
self.lineEdit = QLineEdit(self.centralwidget)
self.lineEdit.setObjectName(u"lineEdit")
self.lineEdit.setGeometry(QRect(80, 30, 331, 20))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QMenuBar(MainWindow)
self.menubar.setObjectName(u"menubar")
self.menubar.setGeometry(QRect(0, 0, 508, 23))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QStatusBar(MainWindow)
self.statusbar.setObjectName(u"statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
# setupUi
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
self.open_button.setText(QCoreApplication.translate("MainWindow", u"\u6253\u5f00", None))
self.label.setText(QCoreApplication.translate("MainWindow", u"URL:", None))
self.lineEdit.setInputMask("")
# retranslateUi
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

Interface to preview

Right click the mouse editor to preview the interface

 

Code execution preview interface effect :

Because the corresponding button function is not edited , So this is just an interface effect , No actual interaction function ;

About how buttons call functions , The corresponding tutorial articles will be updated here in the future :

PyGui、python Language gui Interface setting development ,PySide2、qt designer、PyQt5

版权声明:本文为[Fantasy Lab]所创,转载请带上原文链接,感谢。 https://pythonmana.com/2022/134/202205141438472643.html