将QT的UI界面转换成python文件并调用

该博客介绍了如何在QTcreator或QTdesigner中创建UI界面,并通过pyuic5将其转换为Python代码,在PyCharm中直接调用。首先,通过命令行运行pyuic5将.ui文件转为.py文件,然后在生成的Python文件中定义界面元素和功能,最后在主程序中导入并显示UI。示例代码展示了登录窗口的UI设计和登录、重置按钮的连接。
摘要由CSDN通过智能技术生成

在QTcreator中或者QTdesigner中画好的ui界面转换成python文件,在pycharm中直接调用。

在pycharm中的终端运行命令 :

pyuic5 -o login.py Login.ui 

*.py是对应生成的python文件的名字,*.ui是画的UI界面(对应ui界面的路径)

然后就会生成login.py文件,如下:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'D:\QTCode\chat\Login.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# 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.
import sys

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui_Form, self).__init__()
        self.setupUi(self)
        self.retranslateUi(self)
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(426, 261)
        self.username_label = QtWidgets.QLabel(Form)
        self.username_label.setGeometry(QtCore.QRect(20, 40, 111, 81))
        self.username_label.setLineWidth(1)
        self.username_label.setObjectName("username_label")
        self.password_label = QtWidgets.QLabel(Form)
        self.password_label.setGeometry(QtCore.QRect(30, 120, 131, 71))
        self.password_label.setObjectName("password_label")
        self.username_text = QtWidgets.QPlainTextEdit(Form)
        self.username_text.setGeometry(QtCore.QRect(120, 50, 231, 51))
        self.username_text.setObjectName("username_text")
        self.password_text = QtWidgets.QPlainTextEdit(Form)
        self.password_text.setGeometry(QtCore.QRect(120, 130, 231, 51))
        self.password_text.setPlaceholderText("")
        self.password_text.setObjectName("password_text")
        # self.password_text.show('*')
        self.widget = QtWidgets.QWidget(Form)
        self.widget.setGeometry(QtCore.QRect(90, 200, 251, 41))
        self.widget.setObjectName("widget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.login_button = QtWidgets.QPushButton(self.widget)
        self.login_button.setObjectName("login_button")
        self.horizontalLayout.addWidget(self.login_button)
        self.reset_button = QtWidgets.QPushButton(self.widget)
        self.reset_button.setObjectName("reset_button")
        self.horizontalLayout.addWidget(self.reset_button)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "登陆窗口"))
        self.username_label.setText(_translate("Form", "用户名:"))
        self.password_label.setText(_translate("Form", "密  码:"))
        self.login_button.setText(_translate("Form", "登录"))
        self.reset_button.setText(_translate("Form", "重置"))


调用文件:

# This Python file uses the following encoding: utf-8
import sys
from PySide2.QtWidgets import QApplication, QWidget, QPushButton
from login import *

# class Widget:
#     def __init__(self):
#         lg = Ui_Form()
#         lg.login_button.triggered.clicked.connect(self.loginHandle)
#
#
#
#     def loginHandle(self):
#         print('登录按钮被点击拉!!')

if __name__ == "__main__":
    app = QApplication([])
    # widgit = Widget()
    window = Ui_Form()
    window.show()
    sys.exit(app.exec_())

运行结果:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值