#! /usr/bin/env python
#  -*- coding: utf-8 -*-
#
# GUI module generated by PAGE version 4.26
#  in conjunction with Tcl version 8.6
#    Jan 07, 2020 12:08:36 PM CST  platform: Windows NT

import os
import sys
import create_file
try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

try:
    import ttk
    py3 = False
except ImportError:
    import tkinter.ttk as ttk
    py3 = True

from tkinter.filedialog import askopenfilename
from tkinter.filedialog import askdirectory

def vp_start_gui():
    '''Starting point when module is the main routine.'''
    global val, w, root
    root = tk.Tk()
    Toplevel1 (root)
    root.mainloop()

w = None
def create_Toplevel1(root, *args, **kwargs):
    '''Starting point when module is imported by another program.'''
    global w, w_win, rt
    rt = root
    w = tk.Toplevel (root)
    top = Toplevel1 (w)
    #socks_support.init(w, top, *args, **kwargs)
    return (w, top)

def destroy_Toplevel1():
    global w
    w.destroy()
    w = None

class Toplevel1:
    def __init__(self, top=None):
        '''This class configures and populates the toplevel window.
           top is the toplevel containing window.'''
        _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
        _fgcolor = '#000000'  # X11 color: 'black'
        _compcolor = '#d9d9d9' # X11 color: 'gray85'
        _ana1color = '#d9d9d9' # X11 color: 'gray85'
        _ana2color = '#ececec' # Closest X11 color: 'gray92'

        top.geometry("575x415+761+115")
        top.minsize(116, 1)
        top.maxsize(1924, 1062)
        top.resizable(1, 1)
        top.title("postman文件=>python函数")
        top.configure(background="#d9d9d9")

        self.Label1 = tk.Label(top)
        self.Label1.place(relx=0.09, rely=0.07, height=33, width=80)
        self.Label1.configure(background="#d9d9d9")
        self.Label1.configure(disabledforeground="#a3a3a3")
        self.Label1.configure(foreground="#000000")
        self.Label1.configure(text='''json文件路径:''')

        self.Entry1 = tk.Entry(top)
        self.Entry1.place(relx=0.3, rely=0.072,height=37, relwidth=0.4)


        self.Entry1.configure(background="white")
        self.Entry1.configure(font="TkTextFont")
        self.Entry1.configure(foreground="black")
        self.Entry1.configure(highlightbackground="#d9d9d9")
        self.Entry1.configure(highlightcolor="black")
        self.Entry1.configure(insertbackground="black")
        self.Entry1.configure(selectbackground="#c4c4c4")
        self.Entry1.configure(selectforeground="black")

        self.Button1_1 = tk.Button(top)
        self.Button1_1.place(relx=0.75, rely=0.072, height=37, width=49)
        self.Button1_1.configure(activebackground="#ececec")
        self.Button1_1.configure(activeforeground="#000000")
        self.Button1_1.configure(background="#d9d9d9")
        self.Button1_1.configure(disabledforeground="#a3a3a3")
        self.Button1_1.configure(foreground="#000000")
        self.Button1_1.configure(highlightbackground="#d9d9d9")
        self.Button1_1.configure(highlightcolor="black")
        self.Button1_1.configure(pady="0")
        self.Button1_1.configure(text='''...''')
        self.Button1_1.configure(command=lambda: self.select_file())

        self.Label2 = tk.Label(top)
        self.Label2.place(relx=0.09, rely=0.2, height=37, width=115)
        self.Label2.configure(background="#d9d9d9")
        self.Label2.configure(disabledforeground="#a3a3a3")
        self.Label2.configure(foreground="#000000")
        self.Label2.configure(text='''生成python文件路径:''')

        self.Entry2 = tk.Entry(top)
        self.Entry2.place(relx=0.3, rely=0.2,height=37, relwidth=0.4)

        self.Entry2.configure(background="white")
        self.Entry2.configure(font="TkTextFont")
        self.Entry2.configure(foreground="black")
        self.Entry2.configure(highlightbackground="#d9d9d9")
        self.Entry2.configure(highlightcolor="black")
        self.Entry2.configure(insertbackground="black")
        self.Entry2.configure(selectbackground="#c4c4c4")
        self.Entry2.configure(selectforeground="black")

        self.Button2_1 = tk.Button(top)
        self.Button2_1.place(relx=0.75, rely=0.2, height=37, width=49)
        self.Button2_1.configure(activebackground="#ececec")
        self.Button2_1.configure(activeforeground="#000000")
        self.Button2_1.configure(background="#d9d9d9")
        self.Button2_1.configure(disabledforeground="#a3a3a3")
        self.Button2_1.configure(foreground="#000000")
        self.Button2_1.configure(highlightbackground="#d9d9d9")
        self.Button2_1.configure(highlightcolor="black")
        self.Button2_1.configure(pady="0")
        self.Button2_1.configure(text='''...''')
        self.Button2_1.configure(command=lambda: self.select_dir())

        self.Button1 = tk.Button(top)
        self.Button1.place(relx=0.452, rely=0.337, height=37, width=49)
        self.Button1.configure(activebackground="#ececec")
        self.Button1.configure(activeforeground="#000000")
        self.Button1.configure(background="#d9d9d9")
        self.Button1.configure(disabledforeground="#a3a3a3")
        self.Button1.configure(foreground="#000000")
        self.Button1.configure(highlightbackground="#d9d9d9")
        self.Button1.configure(highlightcolor="black")
        self.Button1.configure(pady="0")
        self.Button1.configure(text='''执行''')
        self.Button1.configure(command=lambda: self.execute())

        self.Text1 = tk.Text(top)
        self.Text1.place(relx=0.017, rely=0.434, relheight=0.535, relwidth=0.963)
        self.Text1.configure(background="white")
        self.Text1.configure(font="TkTextFont")
        self.Text1.configure(foreground="black")
        self.Text1.configure(highlightbackground="#d9d9d9")
        self.Text1.configure(highlightcolor="black")
        self.Text1.configure(insertbackground="black")
        self.Text1.configure(selectbackground="#c4c4c4")
        self.Text1.configure(selectforeground="black")
        self.dir = os.path.dirname(os.path.abspath(__file__))


    def select_file(self):
        postman_file_path=askopenfilename(title='选择一个json文件',
                                          initialdir=self.dir, filetypes=[('Python source file','*.json')])
        self.Entry1.delete(0,"end")
        self.Entry1.insert(0,postman_file_path)

    def select_dir(self):
        postman_dir_path=askdirectory(title='选择文件夹',initialdir=self.dir)
        self.Entry2.delete(0,"end")
        self.Entry2.insert(0, postman_dir_path)

    def execute(self):
        postman_file_path=""
        postman_dir_path=""
        if self.Entry1.get()=='':
            self.Text1.insert(tk.INSERT, "请选择需要生成python文件的json文件" + "\n")
        else:
            postman_file_path=self.Entry1.get()
        if self.Entry2.get()=='':
            self.Text1.insert(tk.INSERT, "请选择选择python文件存放的文件夹" + "\n")
        else:
            postman_dir_path=self.Entry2.get()
        if self.Entry1.get()!='' and self.Entry2.get()!='':
            self.Text1.insert(tk.INSERT, "执行中。。。。。。"+"\n")
            create_file.main(postman_file_path,postman_dir_path,template_file_path=".//postman_template.py")
            self.Text1.insert(tk.INSERT, "执行成功."+"\n")

            if getattr(sys, 'frozen', False):
                pathname = sys._MEIPASS
            else:
                pathname = self.dir
            self.Text1.insert(tk.INSERT, "日志已存放在%s\\log文件夹中" % pathname + "\n")


if __name__ == '__main__':
    vp_start_gui()





