在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_())
运行结果:文章来源:https://www.toymoban.com/news/detail-511051.html
文章来源地址https://www.toymoban.com/news/detail-511051.html
到了这里,关于将QT的UI界面转换成python文件并调用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!