-
Notifications
You must be signed in to change notification settings - Fork 34
/
mkc_filedialog.py
134 lines (115 loc) · 4.56 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
134
# File Choose 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
from utils import lang
def askopenfilename(title="Choose File", filetypes=(("*", "*.*"),)):
return askopenfilenames(title=title, filetypes=filetypes).file
def askdirectory(title="Choose File"):
return askdirectorys(title=title).file
class askopenfilenames(Toplevel):
file = ""
def __init__(self, title="Choose 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(os.path.abspath("/"))
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="选择|Choose", command=self.return_var).pack(fill=X, side=LEFT, padx=5, pady=5)
Button(ff, text=lang.text23, command=self.refs).pack(fill=X, side=LEFT, padx=5, pady=5)
Button(ff, text=lang.cancel, command=self.cancel).pack(fill=X, side=LEFT, padx=5, pady=5)
ff.pack(padx=5, pady=5, fill=X)
self.refs()
self.wait_window()
def p_bind(self, event):
try:
file = self.show.get(self.show.curselection())
except:
file = ""
var = os.path.abspath(os.path.join(self.path.get(), file))
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(os.path.abspath("/"))
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="Choose 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(os.path.abspath("/"))
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="选择|Choose", command=self.return_var).pack(fill=X, side=LEFT, padx=5, pady=5)
Button(ff, text=lang.text23, command=self.refs).pack(fill=X, side=LEFT, padx=5, pady=5)
Button(ff, text=lang.cancel, command=self.cancel).pack(fill=X, side=LEFT, padx=5, pady=5)
ff.pack(padx=5, pady=5, fill=X)
self.refs()
self.wait_window()
def p_bind(self, event):
try:
file = self.show.get(self.show.curselection())
except:
file = ""
var = os.path.abspath(os.path.join(self.path.get(), file))
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(os.path.abspath("/"))
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 = ""
self.file = os.path.join(self.path.get(), file)
self.destroy()
def cancel(self):
self.destroy()