一、用QT设计师绘制界面保存成ui文件,并编写逻辑层
1、打开QTdesigner
2、选择Dialog without Buttons,并单击“创建”按钮。
3、添加控件。
(1)拖拽“Label”控件,在文本属性中添加文字“请输入粘贴JSON文本:”。如图所示:
(2)右键单击窗体,选择“改变样式表”,如图所示:
(3)在“编辑样式表”中输入以下代码:
*{
font-size:16px;
}
如图所示:
(4)拖拽“Plain Text Edit”控件,如图所示:
(5)拖拽“Push Button”控件,在文本属性中添加文字“格式化JSON”,如图所示:
(6)拖拽“Push Button”控件,在文本属性中添加文字“反格式化JSON”,如图所示:
(7)拖拽“Push Button”控件,在文本属性中添加文字“复制JSON内容”,如图所示:
(8)选中如图所示控件,右键单击“水平布局”(快捷键是“ctrl+1”),效果如图:
(9)右键单击窗体,选中“垂直布局”(快捷键为“ctrl+2”),效果如图:
(10)选中窗体,将“windowTitle”属性变为“JSON格式化小工具”,如图所示。
(11)选中窗体,将“objectName”变为“JsonFormatter”。
(12)选中“Plain Text Edit”控件,将“objectName”属性变为“plainTextEdit_json”,如图所示:
(13)选中如图所示控件,将“objectName”属性变为“pushButton_format”,如图所示:
(14)选中如图所示控件,将“objectName”属性变为“pushButton_unformat”,如图所示:
(15)选中如图所示控件,将“objectName”属性变为“pushButton_copyjson”,如图所示:
(16)“ctrl+s”保存文件,将文件名保存为“json_formatter.ui”。
(17)在anaconda中将“json_formatter.ui”文件变为“json_formatter.py”。如下图所示:
(18)生成的“json_formatter.py”代码如下:
# Form implementation generated from reading ui file 'json_formatter.ui'
#
# Created by: PyQt6 UI code generator 6.4.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_JsonFormatter(object):
def setupUi(self, JsonFormatter):
JsonFormatter.setObjectName("JsonFormatter")
JsonFormatter.resize(524, 368)
JsonFormatter.setStyleSheet("*{\n"
" font-size:16px;\n"
"}")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(JsonFormatter)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.label = QtWidgets.QLabel(JsonFormatter)
self.label.setObjectName("label")
self.verticalLayout_2.addWidget(self.label)
self.plainTextEdit_json = QtWidgets.QPlainTextEdit(JsonFormatter)
self.plainTextEdit_json.setObjectName("plainTextEdit_json")
self.verticalLayout_2.addWidget(self.plainTextEdit_json)
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton_format = QtWidgets.QPushButton(JsonFormatter)
self.pushButton_format.setObjectName("pushButton_format")
self.horizontalLayout.addWidget(self.pushButton_format)
self.pushButton_unformat = QtWidgets.QPushButton(JsonFormatter)
self.pushButton_unformat.setObjectName("pushButton_unformat")
self.horizontalLayout.addWidget(self.pushButton_unformat)
self.verticalLayout.addLayout(self.horizontalLayout)
self.pushButton_copyjson = QtWidgets.QPushButton(JsonFormatter)
self.pushButton_copyjson.setObjectName("pushButton_copyjson")
self.verticalLayout.addWidget(self.pushButton_copyjson)
self.verticalLayout_2.addLayout(self.verticalLayout)
self.retranslateUi(JsonFormatter)
QtCore.QMetaObject.connectSlotsByName(JsonFormatter)
def retranslateUi(self, JsonFormatter):
_translate = QtCore.QCoreApplication.translate
JsonFormatter.setWindowTitle(_translate("JsonFormatter", "JSON格式化小工具a"))
self.label.setText(_translate("JsonFormatter", "请输入粘贴JSON文本:"))
self.pushButton_format.setText(_translate("JsonFormatter", "格式化JSON"))
self.pushButton_unformat.setText(_translate("JsonFormatter", "反格式化JSON"))
self.pushButton_copyjson.setText(_translate("JsonFormatter", "复制JSON内容"))
(19)在“simple_computer.py”文件同级建立“simple_computer_main.py”文件。代码如下:
#引入PyQt6相关模块
import json
from PyQt6.QtWidgets import (
QApplication,QDialog,QMessageBox
)
#引入系统相关模块
import sys
#引入同级文件json_formatter.py及相关模块
import json_formatter
#创建MyJsonFormatter类,处理具体的函数功能
class MyJsonFormatter(json_formatter.Ui_JsonFormatter,QDialog):
#创建构造函数
def __init__(self):
#继承父类
super().__init__()
#调用Ui_JsonFormatter类的函数setupUi函数
self.setupUi(self)
#显示窗口
self.show()
#创建控件pushButton_format的单击事件,单击触发函数do_format_json
self.pushButton_format.clicked.connect(self.do_format_json("format"))
# 创建控件pushButton_unformat的单击事件,单击触发函数do_format_json
self.pushButton_unformat.clicked.connect(self.do_format_json("unformat"))
#创建控件pushButton_copyjson的单击事件,触发do_copy_json函数
self.pushButton_copyjson.clicked.connect(self.do_copy_json)
#构造do_copy_json函数
def do_copy_json(self):
#将剪切板中的内容赋值给board
board=QApplication.clipboard()
#将控件toPlainText的值赋值给board
board.setText(self.plainTextEdit_json.toPlainText())
#弹出信息框,证明操作成功
QMessageBox.information(self,"信息提示","复制成功")
#构造do_format_json函数,传入参数“type”,“type”包括“format”、“unformat”和“copy”
def do_format_json(self,type):
#构建inner_format()函数,返回format的情况
def inner_format():
#将控件plainTextEdi的文本属性赋值给json_cont
json_cont=self.plainTextEdit_json.toPlainText()
#判断控件plainTextEdi的文本是否有值,若没有值,则弹出警告框
if not json_cont:
QMessageBox.warning(self,"信息提示","请输入内容")
return
#判断控件plainTextEdit_json的文本内容是否是json格式,若不是,则报错。若是,则进行转化
try:
#判断tpye值是否是“format”,如果是“format”,则进行打包转化,并赋值给变量new_cont
if type=="format":
new_cont=json.dumps(json.loads(json_cont),indent=4,ensure_ascii=False)
else:
#判断type值为“unformat”,进行反格式化转化,并赋值给new_cont
new_cont = json.dumps(json.loads(json_cont),ensure_ascii=False)
#将结果转化结果赋值给plainTextEdit_json
self.plainTextEdit_json.setPlainText(new_cont)
except Exception as e:
QMessageBox.warning(self,"信息提示",f"json文本有问题,加载报错:{e}")
return
#弹出提示框,表明操作成功
QMessageBox.information(self,"信息提示","操作成功")
return inner_format
#创建入口函数
if __name__ == '__main__':
#调用系统应用
app=QApplication(sys.argv)
#app的入口函数
myJsonFormatter=MyJsonFormatter()
#退出系统
sys.exit(app.exec())
二、pyinstaller打包命令
1、在anaconda终端输入以下命令:
pyinstaller -F -w json_formatter_main.py
对文件进行打包。如图;
2、打包后的文件如图所示:
三、小结
本文通过一个PyQt6格式化和反格式化的例子,演示PyQt6的用法。希望对新手初学者有一定的帮助。