#!/usr/bin/env python
# coding=utf-8
import json
import os
import copy
from Tkinter import *
import tkFileDialog
def make_json():
path = pathVar.get()
if os.path.split(path)[1] != "resource":
pathVar.set("路径错误,请重新选择")
return 1
groupname = "preload"
data = dict()
resources = []
groups = []
keys = []
item_dic = dict()
for file in os.listdir(path):
if os.path.isdir(os.path.join(path,file)):
for item in os.listdir(os.path.join(path, file)):
name, tp = item.split(".")
item_dic["name"] = name
if tp in ("jpg","png"):
item_dic["type"] = "image"
keys.append(name)
elif tp in ("mp3","ogg"):
item_dic["type"] = "sound"
keys.append(name)
elif tp == "json":
item_dic["type"] = tp
item_dic["url"] = os.path.join(file+"/"+item)
resources.append(copy.copy(item_dic))
groups.append(dict(name=groupname, keys=",".join(keys)))
data["resources"] = resources
data["groups"] = groups
dump_path = os.path.join(path, "default.res.json")
f = open(dump_path, "w")
json.dump(data, f, indent=4)
f.close()
def choice_fold():
pathVar.set(tkFileDialog.askdirectory())
width = len(pathVar.get())
if width > 15:
entry["width"] = len(pathVar.get())
root = Tk()
root.title("json生成器")
frame = Frame(root)
frame.pack(padx=5, pady=5, fill=BOTH)
pathVar = StringVar()
entry = Entry(frame, textvariable=pathVar, width=15)
choice_btn = Button(frame, text="选择", command=choice_fold)
makejson_btn = Button(frame, text="生成", command=make_json)
entry.grid(row=1, column=1, padx=(5, 0), sticky=(W, E))
choice_btn.grid(row=1, column=2, padx=(10, 5))
makejson_btn.grid(row=1, column=3, padx=5)
frame.columnconfigure(1, weight=1)
root.mainloop()
小工具-生成json文件(py2.7)
最新推荐文章于 2024-08-23 07:57:58 发布