关闭

小工具-生成json文件(py2.7)

标签: python
376人阅读 评论(0) 收藏 举报
分类:
#!/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()

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:96780次
    • 积分:2307
    • 等级:
    • 排名:第16983名
    • 原创:135篇
    • 转载:38篇
    • 译文:1篇
    • 评论:10条
    文章分类
    最新评论