- 已编辑
为了方便快速便捷排队和批量添加内容,我自己用python编写了一个可以编辑json牌堆的可执行程序。目前支持快速新建牌堆和修改现有牌堆。软件简陋还请见谅。
下载链接:https://pan.quark.cn/s/711dd66b2518
源码已贴上。
因为是自用,所以大概率不会有后续更新
`import sys
import json
from PyQt5.QtWidgets import (QApplication, QWidget, QVBoxLayout, QHBoxLayout, QComboBox,
QLineEdit, QTextEdit, QPushButton, QFileDialog, QMessageBox, QLabel)
class JsonEditor(QWidget):
def init(self):
super().init()
self.json_data = {}
self.initUI()
def initUI(self):
self.setWindowTitle('青果骰牌堆编辑工具包')
# 主布局
mainLayout = QVBoxLayout()
# 提示文本
self.placeholderLabel = QLabel("请输入牌堆关键词和牌堆内容以新建牌堆,或导入牌堆文件。")
mainLayout.addWidget(self.placeholderLabel)
# JSON文本显示
self.textDisplay = QTextEdit()
self.textDisplay.textChanged.connect(self.handle_text_changed)
mainLayout.addWidget(self.textDisplay)
# 输入部分布局
inputLayout = QHBoxLayout()
self.newKeyInput = QLineEdit(self)
self.newKeyInput.setPlaceholderText('输入牌堆关键词')
inputLayout.addWidget(self.newKeyInput)
self.newValueInput = QLineEdit(self)
self.newValueInput.setPlaceholderText('输入牌堆内容')
inputLayout.addWidget(self.newValueInput)
mainLayout.addLayout(inputLayout)
# 按钮布局
buttonLayout = QVBoxLayout()
self.addButton = QPushButton('新建牌堆关键词', self)
self.addButton.clicked.connect(self.add_new_key_value)
buttonLayout.addWidget(self.addButton)
# 选择现有键的下拉菜单
self.existingKeyComboBox = QComboBox(self)
buttonLayout.addWidget(self.existingKeyComboBox)
# 新输入框:用于输入牌堆内容
self.newContentForExistingKeyInput = QLineEdit(self)
self.newContentForExistingKeyInput.setPlaceholderText('输入牌堆内容')
buttonLayout.addWidget(self.newContentForExistingKeyInput)
# 更新“添加到选择的牌堆关键词”按钮
self.addToExistingButton = QPushButton('添加到选择的牌堆关键词', self)
self.addToExistingButton.clicked.connect(self.add_to_existing_key)
buttonLayout.addWidget(self.addToExistingButton)
# 批量添加到选择的牌堆关键词按钮
self.batchAddToExistingButton = QPushButton('批量添加到选择的牌堆关键词', self)
self.batchAddToExistingButton.clicked.connect(self.batch_add_to_existing_key_from_file)
buttonLayout.addWidget(self.batchAddToExistingButton)
# 导入导出按钮布局
importExportLayout = QVBoxLayout()
self.importButton = QPushButton('导入 JSON', self)
self.importButton.clicked.connect(self.import_json)
importExportLayout.addWidget(self.importButton)
self.exportButton = QPushButton('导出 JSON', self)
self.exportButton.clicked.connect(self.export_json)
importExportLayout.addWidget(self.exportButton)
# 设置总体布局
mainLayout.addLayout(buttonLayout)
mainLayout.addLayout(importExportLayout)
self.setLayout(mainLayout)
self.update_text_display() # 初次加载后刷新显示
def handle_text_changed(self):
# 暂时断开信号以防止递归
self.textDisplay.blockSignals(True)
if self.textDisplay.toPlainText().strip() == "":
self.json_data = {}
self.update_text_display()
self.textDisplay.blockSignals(False)
def update_text_display(self):
if not self.json_data:
self.textDisplay.clear()
self.placeholderLabel.show()
else:
self.textDisplay.setText(json.dumps(self.json_data, ensure_ascii=False, indent=4))
self.placeholderLabel.hide()
self.update_combo_box()
def update_combo_box(self):
self.existingKeyComboBox.clear()
self.existingKeyComboBox.addItems(self.json_data.keys())
def add_new_key_value(self):
key = self.newKeyInput.text().strip()
value = self.newValueInput.text().strip()
if key and value:
if key in self.json_data:
self.json_data[key].append(value)
else:
self.json_data[key] = [value]
self.update_text_display()
def add_to_existing_key(self):
key = self.existingKeyComboBox.currentText()
value = self.newContentForExistingKeyInput.text().strip()
if not value:
QMessageBox.warning(self, '警告', '内容为空添加失败!')
return
if not key:
QMessageBox.warning(self, '警告', '请先选择一个牌堆关键词!')
return
if value and key in self.json_data:
self.json_data[key].append(value)
self.update_text_display()
def import_json(self):
file_name, _ = QFileDialog.getOpenFileName(self, '打开 JSON 文件', '', 'JSON 文件 (*.json)')
if file_name:
with open(file_name, 'r', encoding='utf-8') as f:
try:
self.json_data = json.load(f)
self.update_text_display()
except json.JSONDecodeError as e:
QMessageBox.critical(self, '错误', f'JSON文件格式不正确!\n错误信息: {str(e)}')
def export_json(self):
file_name, _ = QFileDialog.getSaveFileName(self, '导出 JSON 文件', '', 'JSON 文件 (*.json)')
if file_name:
with open(file_name, 'w', encoding='utf-8') as f:
json.dump(self.json_data, f, ensure_ascii=False, indent=4)
def batch_add_to_existing_key_from_file(self):
key = self.existingKeyComboBox.currentText()
if not key:
QMessageBox.warning(self, '警告', '请先选择一个牌堆关键词!')
return
file_name, _ = QFileDialog.getOpenFileName(self, '选择文本文件', '', '文本文件 (*.txt)')
if file_name:
try:
with open(file_name, 'r', encoding='utf-8') as f:
lines = f.read().splitlines()
if key in self.json_data:
self.json_data[key].extend(lines)
else:
self.json_data[key] = lines
self.update_text_display()
except Exception as e:
QMessageBox.critical(self, '错误', f'无法读取文件:{str(e)}')
if name == ‘main’:
app = QApplication(sys.argv)
editor = JsonEditor()
editor.show()
sys.exit(app.exec_())`