python之PySide2按钮、单行文本框、文本标签

界面效果

python之PySide2按钮、单行文本框、文本标签_第1张图片

designer绘制界面

如果您还没有安装相应的库文件,请参考这里的教程:pyhton之GUI安装PySide2、pyqt5-tools_异想实验室的博客-CSDN博客安装PyQt5(同时安装 PyQt5 和 一些重要的工具,比如 Qt designer)https://blog.csdn.net/weixin_43775400/article/details/124763299?spm=1001.2014.3001.5501

首先使用:Qt designer绘一个界面,

python之PySide2按钮、单行文本框、文本标签_第2张图片

 包含文本标签(label),单行文本框(lineEdit),按钮(pushButton

此处用的是PySide2库,所以安装之后的 designer启动路径为:

安装目录下 Scripts\pyside2-designer.exe

界面绘制完成之后,保存ui文件;

ui文件转换py代码

使用命令,将ui文件,转换成py代码

pyside2-uic main.ui > ui_main.py

注:如果你是虚拟环境中,将ui文件放在你的虚拟环境的工程目录里面,然后cd到工程目录,输入转换命令即可;

python之PySide2按钮、单行文本框、文本标签_第3张图片

代码调用预览界面

打开ui_main.py文件,我们将看到如下类似代码

此时运行是无法预览界面效果的,后面会讲到如何调用

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_())

完整代码:

# -*- 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_())

界面预览

鼠标编辑器右击预览界面

python之PySide2按钮、单行文本框、文本标签_第4张图片

 

代码执行预览界面效果:

python之PySide2按钮、单行文本框、文本标签_第5张图片

因为没有编辑相应的按钮功能函数,所以此时只是一个界面效果,无实际交互功能;

关于按钮如何调用函数功能,以后会在这里更新相应的教程文章:

PyGui、python语言gui界面设置开发,PySide2、qt designer、PyQt5

你可能感兴趣的:(PyGui,ui,qt,开发语言)