Skip to content

Commit 457c3d9

Browse files
author
nanocode38
committed
reconstruction
1 parent 6e56fcd commit 457c3d9

File tree

1 file changed

+85
-70
lines changed

1 file changed

+85
-70
lines changed

main.py

Lines changed: 85 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
from pynput import keyboard
1111
from pynput.keyboard import Key
1212

13-
launcher = 'Chinese'
14-
15-
disable_key = [
13+
DISABLE_KEY = [
1614
Key.cmd, # Windows 键
1715
Key.f1, Key.f2, Key.f3, Key.f4, Key.f5, Key.f6,
1816
Key.f7, Key.f8, Key.f9, Key.f10, Key.f12, Key.f11,
@@ -29,36 +27,45 @@
2927
Key.shift, Key.shift_r, Key.shift_l, # Shift键
3028
]
3129

32-
launcher_key = {
33-
'Chinese':[
34-
'键盘音频文件设置:',
35-
'此文件不是音频文件',
36-
'此路径不是文件',
37-
'退出系统',
38-
'确定',
39-
'关于',
40-
'设置',
41-
'英语English',
42-
"""音效键盘系统
30+
LANGUAGE_LIST = {
31+
'Chinese':{
32+
'sounds set':'键盘音频文件设置:',
33+
'not sounds':'此文件不是音频文件',
34+
'not file':'此路径不是文件',
35+
'exit':'退出系统',
36+
'ok':'确 定',
37+
'about':'关于',
38+
'set':'设置',
39+
'lau':'英语English',
40+
'about str':"""音效键盘系统
4341
属于: nanocode38 Copyright(C)
44-
官方网站: https://github.com/nanocode38/audio_keyboard"""
45-
46-
],
42+
官方网站: https://github.com/nanocode38/audio_keyboard""",
43+
'calc':'取 消',
44+
},
4745
'English':[
4846
'Keyboard audio file settings:',
4947
'This file is not an audio file',
5048
'This path is not a file',
5149
'Exit System',
52-
'OK',
50+
'O K',
5351
'About',
5452
'Settings',
5553
'Chinese中文',
5654
"""Sound keyboard system
5755
Subject to: nanocode38 Copyright (C)
58-
Official website: https://github.com/nanocode38/audio_keyboard"""
56+
Official website: https://github.com/nanocode38/audio_keyboard""",
57+
'Cancel',
5958
]
6059
}
6160

61+
e = {}
62+
i = 0
63+
for key in LANGUAGE_LIST['Chinese'].keys():
64+
e[key] = LANGUAGE_LIST['English'][i]
65+
i += 1
66+
LANGUAGE_LIST['English'] = e
67+
del e, i
68+
6269
continuous_button = {
6370
Key.ctrl_l: False,
6471
Key.ctrl_r: False,
@@ -75,21 +82,23 @@
7582
}
7683

7784
home_path = Path.home()
78-
if not os.path.isdir(home_path / 'sKeyBoard'):
79-
os.mkdir(home_path / 'sKeyBoard')
85+
if not os.path.isdir(home_path / 'AudioKeyBoard') or \
86+
not os.path.isfile(home_path / 'AudioKeyBoard' / 'audio_keyboard_data.dat'):
87+
os.mkdir(home_path / 'AudioKeyBoard')
8088
no_sound = False
8189
sounds_path = r'.\sounds\KeyBoard.mp3'
90+
language = 'Chinese'
8291
else:
83-
with open(home_path / 'sKeyBoard' / 'sounds_data.dat', 'rb') as f:
92+
with open(home_path / 'AudioKeyBoard' / 'audio_keyboard_data.dat', 'rb') as f:
8493
sounds_path = str(pickle.load(f))
85-
with open(home_path / 'sKeyBoard' / 'no_data.dat', 'rb') as f:
94+
language = str(pickle.load(f))
8695
no_sound = bool(pickle.load(f))
8796

8897
def _exit():
8998
global sounds_path, no_sound
90-
with open(home_path / 'sKeyBoard' / 'sounds_data.dat', 'wb') as f:
99+
with open(home_path / 'AudioKeyBoard' / 'audio_keyboard_data.dat', 'wb') as f:
91100
pickle.dump(sounds_path, f)
92-
with open(home_path / 'sKeyBoard' / 'no_data.dat', 'wb') as f:
101+
pickle.dump(language, f)
93102
pickle.dump(no_sound, f)
94103
exit(0)
95104

@@ -111,7 +120,7 @@ def on_press(key):
111120
finally:
112121
if not continuous_button[key]:
113122
continuous_button[key] = True
114-
if key in disable_key or no_sound:
123+
if key in DISABLE_KEY or no_sound:
115124
return
116125
pygame.mixer.init()
117126
pygame.mixer.music.load(sounds_path)
@@ -131,7 +140,7 @@ def out(key):
131140

132141
def get_new_main_root():
133142
global root, sound_image, about_button, exit_button, move_button, settings_button, sound_button, settings_image
134-
global launcher_key, launcher, no_sound, no_sound_image, sound_image, get_about, getsettings, _exit, exit_image
143+
global LANGUAGE_LIST, language, no_sound, no_sound_image, sound_image, get_about, get_settings, _exit, exit_image
135144
root = tk.Tk()
136145
root.overrideredirect(1)
137146
root.wm_attributes('-alpha', 0.8)
@@ -148,10 +157,12 @@ def get_new_main_root():
148157

149158
exit_image = tk.PhotoImage(file=r'.\images\exit.png')
150159

151-
about_button = tk.Button(root, text=launcher_key[launcher][5], command=get_about)
160+
# logo_image = tk.PhotoImage(file=r'.\images\Logo.ico
161+
162+
about_button = tk.Button(root, text=LANGUAGE_LIST[language]['about'], command=get_about)
152163
about_button.place(x=120, y=0)
153164

154-
settings_button = tk.Button(root, image=settings_image, command=getsettings)
165+
settings_button = tk.Button(root, image=settings_image, command=get_settings)
155166
settings_button.pack()
156167

157168
exit_button = tk.Button(root, image=exit_image, command=_exit)
@@ -209,19 +220,20 @@ def loudspeaker_mute():
209220

210221

211222

212-
def getsettings():
213-
global sounds_path
214-
sroot = tk.Tk()
215-
sroot.title(launcher_key[launcher][6])
216-
sroot.wm_attributes('-alpha', 0.8)
217-
sroot.wm_attributes('-topmost', True)
218-
sroot.geometry('400x360+300+250')
219-
sroot.iconbitmap(r'./images/Logo.ico')
220-
221-
label = tk.Label(sroot, text=launcher_key[launcher][0])
223+
def get_settings():
224+
global sounds_path, LANGUAGE_LIST, language
225+
global root
226+
settings_windows = tk.Toplevel(root)
227+
settings_windows.title(LANGUAGE_LIST[language]['set'])
228+
settings_windows.wm_attributes('-alpha', 0.8)
229+
settings_windows.wm_attributes('-topmost', True)
230+
settings_windows.geometry('400x360+300+250')
231+
settings_windows.iconbitmap(r'./images/Logo.ico')
232+
233+
label = tk.Label(settings_windows, text=LANGUAGE_LIST[language]['sounds set'])
222234
label.grid(row=0, column=0)
223235

224-
path_entry = tk.Entry(sroot, bd=3, width=51)
236+
path_entry = tk.Entry(settings_windows, bd=3, width=51)
225237
path_entry.insert(0, sounds_path)
226238
path_entry.place(x=1, y=30)
227239

@@ -235,70 +247,73 @@ def get_message():
235247
if f_path != '':
236248
if os.path.splitext(f_path)[1] not in ('.mp3', '.wav'):
237249
from tkinter import messagebox
238-
messagebox.showerror(title='File Error', message=launcher_key[launcher][1])
250+
messagebox.showerror(title='File Error', message=LANGUAGE_LIST[language]['not sounds'])
239251
else:
240252
sounds_path = f_path
241253
path_entry.delete(0, tk.END)
242254
path_entry.insert(0, f_path)
243255

244-
def s():
256+
def ok():
245257
global sounds_path
246258
if not get:
247259
f_path = path_entry.get()
248260
if f_path != '':
249261
if not os.path.isfile(f_path):
250262
from tkinter import messagebox
251-
messagebox.showerror(title='File Error', message=launcher_key[launcher][2])
263+
messagebox.showerror(title='File Error', message=LANGUAGE_LIST[language]['not file'])
252264
elif os.path.splitext(f_path)[1] not in ('.mp3', '.wav'):
253265
from tkinter import messagebox
254-
messagebox.showerror(title='File Error', message=launcher_key[launcher][1])
266+
messagebox.showerror(title='File Error', message=LANGUAGE_LIST[language]['no sounds'])
255267
else:
256268
sounds_path = f_path
257-
sroot.destroy()
269+
settings_windows.destroy()
258270

259-
path_button = tk.Button(sroot, text='. . .', command=get_message)
260-
ok_button = tk.Button(sroot, text=launcher_key[launcher][4], command=s)
271+
path_button = tk.Button(settings_windows, text='. . .', command=get_message)
272+
ok_button = tk.Button(settings_windows, width=10, text=LANGUAGE_LIST[language]['ok'], command=ok)
273+
no_button = tk.Button(settings_windows, width=10, text=LANGUAGE_LIST[language]['calc'],
274+
command=settings_windows.destroy)
261275
path_button.place(x=370, y=25)
262-
ok_button.place(x=10, y=59)
276+
ok_button.place(x=250, y=330)
277+
no_button.place(x=320, y=330)
263278

264-
exit_button = tk.Button(sroot, text=launcher_key[launcher][3], command=_exit)
279+
exit_button = tk.Button(settings_windows, text=LANGUAGE_LIST[language]['exit'], command=_exit)
265280
exit_button.place(x=250, y=100)
266281

267-
def launcher_k():
282+
def changing_over_language():
268283
global root
269-
global launcher
270-
if launcher == 'Chinese':
271-
launcher = 'English'
284+
global language
285+
if language == 'Chinese':
286+
language = 'English'
272287
else:
273-
launcher = 'Chinese'
288+
language = 'Chinese'
274289
root.destroy()
275-
sroot.destroy()
276290
get_new_main_root()
277291

278-
launcher_button = tk.Button(sroot, text=launcher_key[launcher][7], command=launcher_k)
279-
launcher_button.place(x=100, y=200)
292+
language_button = tk.Button(settings_windows, text=LANGUAGE_LIST[language]['lau'], command=changing_over_language)
293+
language_button.place(x=100, y=200)
280294

281295

282-
sroot.update()
283-
sroot.mainloop()
296+
settings_windows.update()
297+
settings_windows.mainloop()
284298

285299

286300
def get_about():
287-
sroot = tk.Tk()
288-
sroot.title(launcher_key[launcher][5])
289-
sroot.wm_attributes('-alpha', 0.8)
290-
sroot.wm_attributes('-topmost', True)
291-
sroot.geometry('400x360+300+250')
292-
sroot.iconbitmap(r'./images/Logo.ico')
301+
global root, language, LANGUAGE_LIST
302+
about_windows = tk.Toplevel(root)
303+
about_windows.title(LANGUAGE_LIST[language]['about'])
304+
about_windows.wm_attributes('-alpha', 0.8)
305+
about_windows.wm_attributes('-topmost', True)
306+
about_windows.geometry('400x360+300+250')
307+
about_windows.iconbitmap(r'./images/Logo.ico')
293308

294-
about_text = launcher_key[launcher][8]
309+
about_text = LANGUAGE_LIST[language]['about str']
295310

296-
label = tk.Label(sroot, text=about_text)
311+
label = tk.Label(about_windows, text=about_text)
297312
label.grid(row=0, column=0)
298313

299314

300315
root = tk.Tk()
301-
root.overrideredirect(1)
316+
root.overrideredirect(True)
302317
root.wm_attributes('-alpha', 0.8)
303318
root.wm_attributes('-toolwindow', True)
304319
root.wm_attributes('-topmost', True)
@@ -313,10 +328,10 @@ def get_about():
313328

314329
exit_image = tk.PhotoImage(file=r'.\images\exit.png')
315330

316-
about_button = tk.Button(root, text=launcher_key[launcher][5], command=get_about)
331+
about_button = tk.Button(root, text=LANGUAGE_LIST[language]['about'], command=get_about)
317332
about_button.place(x=120, y=0)
318333

319-
settings_button = tk.Button(root, image=settings_image, command=getsettings)
334+
settings_button = tk.Button(root, image=settings_image, command=get_settings)
320335
settings_button.pack()
321336

322337
exit_button = tk.Button(root, image=exit_image, command=_exit)

0 commit comments

Comments
 (0)