Qt设计师设计UI界面,记得修改窗体和控件的object属性
![](https://img-blog.csdnimg.cn/img_convert/f152cfee70f650feca0daf6063f63cee.png)
从ui文件生成py文件
(pyqt6)) C:\Users\XX\Desktop\pythonProject\chapter03>pyuic6 json_formatter.ui -o json_formatter.py
chapter03文件夹新建json_formatter_main.py文件:
import json
from PyQt6.QtWidgets import QApplication, QWidget, QDialog, QMessageBox
import sys
import json_formatter
class MyJsonFormatter(json_formatter.Ui_JsonFormatter, QDialog):
def __init__(self):
super(MyJsonFormatter, self).__init__()
self.setupUi(self)
self.show()
self.pushButton_format.clicked.connect(self.do_format_json("format"))
self.pushButton_unformat.clicked.connect(self.do_format_json("unformat"))
self.pushButton_copyjson.clicked.connect(self.do_copy_json)
def do_format_json(self, type):
def inner_format():
json_cont = self.plainTextEdit_json.toPlainText()
if not json_cont:
QMessageBox.warning(self, "信息提示", "请输入内容")
return
try:
if type == "format":
new_cont = json.dumps(json.loads(json_cont), indent=4, ensure_ascii=False)
else:
new_cont = json.dumps(json.loads(json_cont), ensure_ascii=False)
self.plainTextEdit_json.setPlainText(new_cont)
except Exception as e:
QMessageBox.warning(self, "信息提示", f"json文本有问题,加载报错{e}")
return
QMessageBox.information(self, "信息提示", "操作成功")
return inner_format
def do_copy_json(self):
board = QApplication.clipboard()
board.setText(self.plainTextEdit_json.toPlainText())
QMessageBox.information(self, "信息提示", "复制成功")
if __name__ == '__main__':
app = QApplication(sys.argv)
myJsonFormatter = MyJsonFormatter()
sys.exit(app.exec())
运行json_formatter_main.py,输入json字符串,点击格式化json
![](https://img-blog.csdnimg.cn/img_convert/3f6656b576788cc60c450de2ba9c18a4.png)
点击反格式化json
![](https://img-blog.csdnimg.cn/img_convert/d7dea63f473949c09e0d3236a7c85237.png)
点击复制json内容
![](https://img-blog.csdnimg.cn/img_convert/d6d05e18b29fb106a98e374da122136c.png)
打包成.exe文件
pyinstaller -F -w json_formatter_main.py