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

#!/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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值