这是我用的工具 因为是最后一个免费版 后续会上传网盘地址
1.安装后打开界面 点击新建
2.选择项目的类型 我选择的是QT for Python
3.输入项目名称和项目路径
4.随便输入一个初始化文件 因为我不用软件提供的Python环境 自己选择 这里用idea作为开发IDE
5.下一步 完成 之后 会发现生成两个文件 不用管
6.在项目文件夹 右键 选择add new
7.我选的是 QT -> QT Designer Form
8.默认下一步
9.这个名字后续会用到
10.直接按照 元素拖上去即可
12.保存后 右键新建的ui文件 选择在资源管理器打开
13.在这里右键选择 终端 也可以 用cmd 总之要把位置切换到ui文件所在的目录
14.这里需要安装一下 pyuic6 环境安装自行百度 安装完输入指令
pyui6 -o 生成的文件名.py 对应的ui文件名.ui 回车即可
15.
16.把py文件 放到ide的目录下 并且新建一个 启动程序
17.login_demo.py的代码如下
----------------------------------------------------------------------------------------------------------------------------------------
from PyQt6 import QtCore, QtGui, QtWidgets
# 这一行有调整 方便更加识别 这个名称可以在创建窗口文件时更改
class login_form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(213, 218)
self.pushButton = QtWidgets.QPushButton(parent=Form)
self.pushButton.setGeometry(QtCore.QRect(60, 150, 75, 23))
self.pushButton.setObjectName("pushButton")
self.widget = QtWidgets.QWidget(parent=Form)
self.widget.setGeometry(QtCore.QRect(20, 50, 165, 22))
self.widget.setObjectName("widget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(parent=self.widget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(parent=self.widget)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout.addWidget(self.lineEdit)
self.widget1 = QtWidgets.QWidget(parent=Form)
self.widget1.setGeometry(QtCore.QRect(20, 100, 165, 22))
self.widget1.setObjectName("widget1")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.widget1)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtWidgets.QLabel(parent=self.widget1)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.lineEdit_2 = QtWidgets.QLineEdit(parent=self.widget1)
self.lineEdit_2.setObjectName("lineEdit_2")
self.horizontalLayout_2.addWidget(self.lineEdit_2)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "登录"))
self.label.setText(_translate("Form", "账号"))
self.label_2.setText(_translate("Form", "密码"))
----------------------------------------------------------------------------------------------------------------------------------------
18.启动程序的代码如下:login_test.py
----------------------------------------------------------------------------------------------------------------------------------------
from PyQt6.QtWidgets import QMainWindow, QApplication, QMessageBox
from login_demo import login_form
import sys
class login_demo(QMainWindow, login_form):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.login)
def login(self):
# 判断账号密码 通过request库调用api实现权限系统的对接
if self.lineEdit.text() == "demo" and self.lineEdit_2.text() == "demo":
QMessageBox.information(self, "提示信息", "登录成功")
else:
QMessageBox.information(self, "提示信息", "账号密码错误")
if __name__ == '__main__':
# 初始化框架
app = QApplication(sys.argv)
# 调用登录窗口
login_demo = login_demo()
login_demo.show()
# 销毁窗口
sys.exit(app.exec())
----------------------------------------------------------------------------------------------------------------------------------------
19.运行login_test.py 就可以启动测试
20.
21.这里可以再补充一点知识点 有些喜欢折腾的可能会百度 怎么把这个程序打包成exe
这里需要增加一个环境 pyinstaller.exe 还是自行百度安装 主要有两种用法 -F -D
-F 打包成单个exe -D 文件比较多
22.使用: 打开Python文件的目录 运行cmd
23.输入指令 : pyinstaller -F -w login_demo.py 等待一会
24.提示如下 就是打包成功 去dist 目录查看exe 如果运行提示缺少环境 先把系统Python环境补齐 再次打包
25.不要问为什么没有运行测试 可能是不存在引导窗口的问题 有兴趣的继续探索 我就不处理了
打包和本文到此结束
- 这里的对其可以 选择标签和输入框 选择水平布局 (老图 没按钮)