前言:为了方便测试员编辑一些json文件信息,我打算提供一个简洁清晰的ui界面来提高工作效率。该ui基于轻量化tkinter实现。大致效果如下图所示:
前置环境:python3
import tkinter as tk
from tkinter import messagebox
import json
import os
import _locale
_locale._getdefaultlocale = (lambda *args: ['zh_CN', 'utf8'])
# 配置文件路径
CONFIG_FILE = '..\Config\presentation.json'
CONFIG_FILE2 = '..\Config\stage_test_case.json'
CONFIG_FILE和CONFIG_FILE2为需要读取的json文件,路径可根据自身情况来修改。关于json文件的读写如下:
def save_config():#指定需要更改的json元素并获取保存
config = {
"start_command": homepage_connect.get(),
"details_command": details_connect.get()
}
with open(CONFIG_FILE2, 'r', encoding='UTF-8') as file:
config2 = json.load(file)
config2['case400']['caseSettings']['robot_task'] = homepage_slide.get()
config2['case401']['caseSettings']['robot_task'] = details_slide.get()
config2['case402']['caseSettings']['robot_task'] = details20s_slide.get()
config2['case403']['caseSettings']['robot_task'] = menu_slide.get()
try:
with open(CONFIG_FILE, 'w', encoding='UTF-8') as file:
json.dump(config, file, ensure_ascii=False, sort_keys=False, indent=2)
messagebox.showinfo("成功", "接口配置已成功保存")
with open(CONFIG_FILE2, 'w', encoding='UTF-8') as file:
json.dump(config2, file, ensure_ascii=False, sort_keys=False, indent=2)
messagebox.showinfo("成功", "文件配置已成功保存")
except Exception as e:
messagebox.showerror("错误", f"保存失败: {e}")
def load_config():#读取json文件
if os.path.exists(CONFIG_FILE):
try:
with open(CONFIG_FILE, 'r', encoding='UTF-8') as file:
config = json.load(file)
homepage_connect.delete(0, tk.END)
homepage_connect.insert(0, config.get('start_command', ''))
details_connect.delete(0, tk.END)
details_connect.insert(0, config.get('details_command', ''))
messagebox.showinfo("成功", "接口配置已成功加载")
except Exception as e:
messagebox.showerror("错误", f"加载失败: {e}")
if os.path.exists(CONFIG_FILE2):
try:
with open(CONFIG_FILE2, 'r', encoding='UTF-8') as file:
config2 = json.load(file)
homepage_slide.delete(0, tk.END)
homepage_slide.insert(0, config2['case400']['caseSettings'].get('robot_task', ''))
details_slide.delete(0, tk.END)
details_slide.insert(0, config2['case401']['caseSettings'].get('robot_task', ''))
details20s_slide.delete(0, tk.END)
details20s_slide.insert(0, config2['case402']['caseSettings'].get('robot_task', ''))
menu_slide.delete(0, tk.END)
menu_slide.insert(0, config2['case403']['caseSettings'].get('robot_task', ''))
messagebox.showinfo("成功", "文件配置已成功加载")
except FileNotFoundError:
messagebox.showwarning("警告", "配置文件不存在")
except Exception as e:
messagebox.showerror("错误", f"加载失败: {e}")
创建窗口来调用上述函数实现交互:
# 创建主窗口
root = tk.Tk()
root.title("演示模式接口命令配置工具")
# 创建一个文本框用于输入启动命令
start_command_label = tk.Label(root, text="演示模式主页接口:")
start_command_label.pack(pady=5)
homepage_connect = tk.Entry(root, width=80)
homepage_connect.pack(pady=5)
# 创建一个文本框用于输入详情页命令
details_command_label = tk.Label(root, text="演示模式详情页接口:")
details_command_label.pack(pady=5)
details_connect = tk.Entry(root, width=80)
details_connect.pack(pady=5)
# 创建一个文本框用于输入另一个启动命令
another_start_command_label = tk.Label(root, text="演示模式主页,修改末尾竖滑为1横滑为2:")
another_start_command_label.pack(pady=5)
homepage_slide = tk.Entry(root, width=80)
homepage_slide.pack(pady=5)
# 创建一个文本框用于输入详情页滑动命令
another_details_command_label = tk.Label(root, text="演示模式详情页,修改末尾竖滑为1横滑为2:")
another_details_command_label.pack(pady=5)
details_slide = tk.Entry(root, width=80)
details_slide.pack(pady=5)
# 创建一个文本框用于输入详情页20s滑动命令
another_details_command_label = tk.Label(root, text="演示模式详情页20s,修改末尾竖滑为1横滑为2:")
another_details_command_label.pack(pady=5)
details20s_slide = tk.Entry(root, width=80)
details20s_slide.pack(pady=5)
# 创建一个文本框用于输入详情页20s滑动命令
another_details_command_label = tk.Label(root, text="演示模式菜单栏详情页,修改末尾竖滑为1横滑为2:")
another_details_command_label.pack(pady=5)
menu_slide = tk.Entry(root, width=80)
menu_slide.pack(pady=5)
# 创建一个按钮用于保存配置
save_button = tk.Button(root, text="保存配置", command=save_config)
save_button.pack(pady=10)
# 创建一个按钮用于加载配置
load_button = tk.Button(root, text="加载配置", command=load_config)
load_button.pack(pady=10)
# 加载初始配置
load_config()
# 运行主循环
root.mainloop()
--end
标签:界面设计,tkinter,Python,slide,command,tk,pady,root,details From: https://blog.csdn.net/weixin_54361902/article/details/142387153