forked from lly-q/MIO-KITCHEN-SOURCE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkc_filedialog.py
133 lines (115 loc) · 4.5 KB
/
mkc_filedialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File Chose Extra Module For MIO-KITCHEN
import os
from tkinter import Toplevel, Listbox, X, BOTH, LEFT, END, StringVar
from tkinter.ttk import Button, Entry, Frame, Combobox
def askopenfilename(title="Chose File", filetypes=(("*", "*.*"),)):
return askopenfilenames(title=title, filetypes=filetypes).file
def askdirectory(title="Chose File"):
return askdirectorys(title=title).file
class askopenfilenames(Toplevel):
file = ""
def __init__(self, title="Chose File", filetypes=(("*", "*.*"),)):
super().__init__()
self.title(title)
self.protocol("WM_DELETE_WINDOW", self.cancel)
self.filetypes = filetypes
self.type = Combobox(self, state='readonly', values=[i[1] for i in self.filetypes])
try:
self.type.current(0)
finally:
pass
self.type.pack(fill=X, padx=5, pady=5)
self.path = StringVar()
self.paths = Entry(self, textvariable=self.path)
self.paths.bind("<Return>", self.p_bind)
self.path.set("/")
self.paths.pack(fill=X, padx=5, pady=5)
self.show = Listbox(self, activestyle='dotbox', highlightthickness=0)
self.show.bind("<Double-Button-1>", self.p_bind)
self.show.pack(fill=BOTH, padx=5, pady=5)
ff = Frame(self)
Button(ff, text="选择|Chose", command=self.return_var).pack(fill=X, side=LEFT, padx=5, pady=5)
Button(ff, text="刷新|Refresh", command=self.refs).pack(fill=X, side=LEFT, padx=5, pady=5)
Button(ff, text="取消|Cancel", command=self.cancel).pack(fill=X, side=LEFT, padx=5, pady=5)
ff.pack(padx=5, pady=5, fill=X)
self.wait_window()
def p_bind(self, event):
try:
file = self.show.get(self.show.curselection())
except:
file = ""
var = os.path.join(self.path.get(), file)
var = os.path.abspath(var)
if os.path.isdir(var):
self.path.set(var)
self.refs()
elif os.path.isfile(var):
self.return_var()
def refs(self):
self.show.delete(0, END)
if not self.path.get():
self.path.set("/")
f, e = self.type.get().replace("*", "").split(".")
for f in os.listdir(self.path.get()):
if f.startswith(f) and f.endswith(e):
self.show.insert(END, f)
def return_var(self):
try:
file = self.show.get(self.show.curselection())
except:
file = ""
var = os.path.join(self.path.get(), file)
if os.path.isfile(var):
self.file = var
self.destroy()
def cancel(self):
self.destroy()
class askdirectorys(Toplevel):
file = ""
def __init__(self, title="Chose File"):
super().__init__()
self.title(title)
self.protocol("WM_DELETE_WINDOW", self.cancel)
self.path = StringVar()
self.paths = Entry(self, textvariable=self.path)
self.paths.bind("<Return>", self.p_bind)
self.path.set("/")
self.paths.pack(fill=X, padx=5, pady=5)
self.show = Listbox(self, activestyle='dotbox', highlightthickness=0)
self.show.bind("<Double-Button-1>", self.p_bind)
self.show.pack(fill=BOTH, padx=5, pady=5)
ff = Frame(self)
Button(ff, text="选择|Chose", command=self.return_var).pack(fill=X, side=LEFT, padx=5, pady=5)
Button(ff, text="刷新|Refresh", command=self.refs).pack(fill=X, side=LEFT, padx=5, pady=5)
Button(ff, text="取消|Cancel", command=self.cancel).pack(fill=X, side=LEFT, padx=5, pady=5)
ff.pack(padx=5, pady=5, fill=X)
self.wait_window()
def p_bind(self, event):
try:
file = self.show.get(self.show.curselection())
except:
file = ""
var = os.path.join(self.path.get(), file)
var = os.path.abspath(var)
if os.path.isdir(var):
self.path.set(var)
self.refs()
elif os.path.isfile(var):
self.return_var()
def refs(self):
self.show.delete(0, END)
if not self.path.get():
self.path.set("/")
for f in os.listdir(self.path.get()):
if os.path.isdir(os.path.join(self.path.get(), f)):
self.show.insert(END, f)
def return_var(self):
try:
file = self.show.get(self.show.curselection())
except:
file = ""
var = os.path.join(self.path.get(), file)
self.file = var
self.destroy()
def cancel(self):
self.destroy()