Repository navigation
Expand file tree
/
Copy pathgui.py
More file actions
183 lines (154 loc) · 5.83 KB
/
Copy pathgui.py
File metadata and controls
183 lines (154 loc) · 5.83 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import subprocess
import webbrowser
from datetime import datetime
from tkinter import (
messagebox,
)
from src.simpletkgrid import (
Config,
RootWindow,
SubWindow,
dialog,
)
from .define import (
APPNAME_FULL,
URL,
messages,
)
def main(config: Config, args) -> None:
class AboutWindow(SubWindow):
def __init__(self) -> None:
_ret = super().__init__(title="About")
self.labels.add(APPNAME_FULL, fontscale="big")
self.labels.add(f"Website: {URL}")
self.buttons.add("Open Website", self.open_url)
self.buttons.add("Close[ESC]", self.close)
self.bind("<Escape>", self.close)
return _ret
def open_url(event=None) -> None:
_ = webbrowser.open_new(URL)
return None
class ConfigWindow(SubWindow):
def __init__(self) -> None:
_ret = super().__init__(title="Config", fontsize=10, maxcolumn=1)
self.entries.defaultwidth = 100
def _filediag(
k: str,
mode: str = "f",
) -> None:
_path = dialog.askopenpath(self.entries.get(k), mode=mode)
if _path is not None:
self.entries.set(k, _path)
return None
for k, v in config.to_dict().items():
self.labels.add(f"{k}({type(v).__name__}): {messages.config.__getattribute__(k)}", fullspan=True)
self.entries.add(k, str(v))
# if type(config.default[k]).__name__ in ["Path"]:
# self.buttons.add("Browse", lambda: _diag(k, "file"))
if k == "workdir":
self.buttons.add("Browse", lambda: _filediag("workdir", "dir"), fullspan=True)
# blank row
self.labels.add("", fullspan=True)
self.buttons.add("Save[Enter]", self.save, fullspan=True)
self.buttons.add("Cancel[ESC]", self.close, fullspan=True)
self.bind("<Return>", self.save)
self.bind("<Escape>", self.close)
return _ret
def save(self, event=None) -> None:
_changed = False
for k, entry in self.entries.items():
v = entry.get()
if str(v) != str(config[k]):
_changed = True
config[k] = v
if not _changed:
messagebox.showinfo("Config", "Nothing changed.")
self.close()
elif messagebox.askyesno("Save config", f"Save to configfile and reload datafile?"):
config.save(section=args.config_section, mode="overwrite")
config.cast()
messagebox.showinfo("Config", "Saved.")
self.close()
return None
class TestWindow01(SubWindow):
def __init__(self) -> None:
_ret = super().__init__(title="Config", fontsize=10, button=True, radiobutton=True)
self.stringvars.add("key1", defaultvalue="")
self.radiobuttons.add("A A A", "A123", self.stringvars["key1"])
self.radiobuttons.add("BBBBB", "B123", self.stringvars["key1"])
self.radiobuttons.add("CCC C", "C123", self.stringvars["key1"])
self.buttons.add("Update[Enter]", self.update)
self.buttons.add("Cancel[ESC]", self.close)
self.bind("<Return>", self.update)
self.bind("<Escape>", self.close)
return _ret
def update(self, event=None) -> None:
val = self.stringvars.get("key1")
if len(val) > 0:
root.stringvars.set("test01", val)
self.close()
else:
messagebox.showwarning("Warning", f"Please select.")
return None
def _do_nothing(event=None) -> None:
return None
def _open(event=None) -> None:
subprocess.Popen(["explorer", config["workdir"]], shell=True)
return None
def _export(event=None) -> None:
now = datetime.now().strftime("%Y%m%d%H%M%S")
filename = "hoge_" + now
extention = ".txt"
dialog.asksave(
"hogefuga:" + now,
filetypes=[("Text file", extention)],
initialdir=config["workdir"],
initialfile=filename+extention,
)
return None
def _about(event=None):
AboutWindow()
return None
def _config(event=None):
ConfigWindow()
return None
def _test01(event=None):
TestWindow01()
return None
root = RootWindow(
title=APPNAME_FULL,
resizable=False,
maxcolumn=4,
padding=20,
defaultwidth=25,
)
root.buttons.add("[O]pen workdir", _open)
root.lf()
root.buttons.add("Export to file", _export)
root.lf()
root.stringvars.add("test01")
root.buttons.add("Test01", _test01)
root.labels.add(root.stringvars["test01"])
root.lf()
root.labels.add("Label(fontscale=2.0)", fontscale=2.0, fullspan=True)
root.labels.add("Label(fontscale='big')", fontscale="big", fullspan=True)
root.labels.add("Label(normal)", fullspan=True)
root.labels.add("Label(fontscale='small')", fontscale="small", fullspan=True)
root.buttons.add("Btn1(columnspan=3)", _do_nothing, columnspan=3)
root.buttons.add("Btn2", _do_nothing)
root.buttons.add("Btn3(width=20)", _do_nothing, width=20)
root.buttons.add("Btn4", _do_nothing)
root.buttons.add("Btn5", _do_nothing)
root.lf(2)
root.buttons.add("About[F1]", _about)
root.buttons.add("[C]onfig", _config)
root.buttons.add("Reload[F5]", _do_nothing)
root.buttons.add("Quit[Esc]", root.close)
root.lf()
# keybind
root.bind("o", _open)
root.bind("c", _config)
root.bind("<F1>", _about)
root.bind("<F5>", _do_nothing)
root.bind("<Escape>", root.close)
root.mainloop()