4545""" ,
4646 'calc' :'取 消' ,
4747 'language text' : '语言设置(Language):' ,
48- 'reset' : '恢复原路径'
48+ 'reset' : '恢复原路径' ,
49+ 'black mode' : '深色模式' ,
50+ 'white mode' : '灯光模式' ,
51+ 'mode title' : '主题'
4952},
5053 'English' :[
5154 'Keyboard audio file settings:' ,
6366""" ,
6467 'Cancel' ,
6568 'Language(语言设置):' ,
66- 'recover'
69+ 'recover' ,
70+ 'Dark mode' ,
71+ 'Light mode' ,
72+ 'Theme' ,
6773 ]
6874}
6975
94100home_path = Path .home ()
95101if not os .path .isdir (home_path / 'AudioKeyBoard' ) or \
96102 not os .path .isfile (home_path / 'AudioKeyBoard' / 'audio_keyboard_data.dat' ):
97- os .mkdir (home_path / 'AudioKeyBoard' )
103+ try :
104+ os .mkdir (home_path / 'AudioKeyBoard' )
105+ except FileExistsError :
106+ pass
98107 no_sound = False
99108 sounds_path = r'.\sounds\KeyBoard.mp3'
100109 language = 'Chinese'
110+ theme = 'black'
111+ theme_color = 'white'
101112else :
102113 with open (home_path / 'AudioKeyBoard' / 'audio_keyboard_data.dat' , 'rb' ) as f :
103114 sounds_path = str (pickle .load (f ))
104115 language = str (pickle .load (f ))
105116 no_sound = bool (pickle .load (f ))
117+ theme = str (pickle .load (f ))
118+ if theme == 'white' :
119+ theme_color = 'black'
120+ elif theme == 'black' :
121+ theme_color = 'white'
106122
107123
108124sounds_path = os .path .abspath (sounds_path )
125+
126+ def update_theme ():
127+ global theme , theme_color
128+ if theme == 'black' :
129+ theme_color = 'white'
130+ else :
131+ theme_color = 'black'
109132def _exit ():
110- global sounds_path , no_sound
133+ global sounds_path , no_sound , theme
111134 with open (home_path / 'AudioKeyBoard' / 'audio_keyboard_data.dat' , 'wb' ) as f :
112135 pickle .dump (sounds_path , f )
113136 pickle .dump (language , f )
114137 pickle .dump (no_sound , f )
138+ pickle .dump (theme , f )
115139 exit (0 )
116140
117141# 定义回调函数,在按键按下时执行
@@ -154,43 +178,51 @@ def out(key):
154178def get_new_main_root (isnooroot = True ):
155179 global root , sound_image , about_button , exit_button , move_button , settings_image
156180 global LANGUAGE_LIST , language , no_sound , no_sound_image , sound_image , get_about
157- global logo , get_settings , _exit , exit_image , settings_button , sound_button
181+ global logo , get_settings , _exit , exit_image , settings_button , sound_button , theme , theme_color
158182 if isnooroot :
159183 root = tk .Tk ()
184+ root .configure (background = theme )
160185 root .overrideredirect (True )
161186 root .wm_attributes ('-alpha' , 0.7 )
162187 root .wm_attributes ('-toolwindow' , True )
163188 root .wm_attributes ('-topmost' , True )
164189 root .geometry ('175x25+500+600' )
165190 root .update ()
166191
167- sound_image = tk .PhotoImage (file = r".\images\sound_button.png" )
168- no_sound_image = tk .PhotoImage (file = r".\images\no_sound_button.png" )
169- settings_image = tk .PhotoImage (file = r'.\images\settings.png' )
192+ if theme == 'white' :
193+ sound_image = tk .PhotoImage (file = r".\images\sound_button.png" )
194+ no_sound_image = tk .PhotoImage (file = r".\images\no_sound_button.png" )
195+ settings_image = tk .PhotoImage (file = r'.\images\settings.png' )
196+ else :
197+ sound_image = tk .PhotoImage (file = r".\images\sound_button black.png" )
198+ no_sound_image = tk .PhotoImage (file = r".\images\no_sound_button black.png" )
199+ settings_image = tk .PhotoImage (file = r'.\images\settings black.png' )
170200 exit_image = tk .PhotoImage (file = r'.\images\exit.png' )
171201 logo_image = tk .PhotoImage (file = r'.\images\logo.png' )
172202
173203
174- settings_button = tk .Button (root , image = settings_image , command = get_settings )
204+ settings_button = tk .Button (root , image = settings_image , command = get_settings , bg = theme , fg = theme_color ,
205+ activebackground = theme , activeforeground = theme )
175206 settings_button .place (x = 10 , y = 0 )
176207
177- exit_button = tk .Button (root , image = exit_image , command = _exit )
208+ exit_button = tk .Button (root , image = exit_image , command = _exit , bg = theme , fg = theme_color ,
209+ activebackground = theme , activeforeground = theme )
178210 exit_button .place (x = 39 , y = 0 )
179211
180- move_button = tk .Button (root )
212+ move_button = tk .Button (root , bg = theme , fg = theme_color , activebackground = theme , activeforeground = theme )
181213 # 绑定按下和松开事件
182214 move_button .bind ("<ButtonPress-1>" , start_move )
183215 move_button .bind ("<ButtonRelease-1>" , stop_move )
184216 move_button .bind ("<B1-Motion>" , on_motion )
185217 move_button .place (x = - 1 , y = 0 )
186218
187219 if no_sound :
188- sound_button = tk .Button (root , image = no_sound_image , command = loudspeaker_mute )
220+ sound_button = tk .Button (root , image = no_sound_image , command = loudspeaker_mute , bg = theme , fg = theme_color , activebackground = theme , activeforeground = theme )
189221 else :
190- sound_button = tk .Button (root , image = sound_image , command = loudspeaker_mute )
222+ sound_button = tk .Button (root , image = sound_image , command = loudspeaker_mute , bg = theme , fg = theme_color , activebackground = theme , activeforeground = theme )
191223 sound_button .place (x = 110 , y = - 1 )
192224
193- logo = tk .Button (root , image = logo_image , command = get_about )
225+ logo = tk .Button (root , image = logo_image , command = get_about , bg = theme , fg = theme_color , activebackground = theme , activeforeground = theme )
194226 logo .place (x = 146 , y = - 3 )
195227
196228 root .mainloop ()
@@ -234,18 +266,19 @@ def loudspeaker_mute():
234266
235267def get_settings ():
236268 global sounds_path , LANGUAGE_LIST
237- global root , language
269+ global root , language , theme , theme_color
238270 settings_windows = tk .Toplevel (root )
271+ settings_windows .configure (background = theme )
239272 settings_windows .title (LANGUAGE_LIST [language ]['set' ])
240273 settings_windows .wm_attributes ('-alpha' , 0.8 )
241274 settings_windows .wm_attributes ('-topmost' , True )
242275 settings_windows .geometry ('400x360+300+250' )
243276 settings_windows .iconbitmap (r'./images/Logo.ico' )
244277
245- label = tk .Label (settings_windows , text = LANGUAGE_LIST [language ]['sounds set' ])
278+ label = tk .Label (settings_windows , text = LANGUAGE_LIST [language ]['sounds set' ], bg = theme , fg = theme_color )
246279 label .grid (row = 0 , column = 0 )
247280
248- path_entry = tk .Entry (settings_windows , bd = 3 , width = 51 )
281+ path_entry = tk .Entry (settings_windows , bd = 3 , width = 51 , bg = theme , fg = theme_color )
249282 if sounds_path [1 ] != ':' :
250283 path_entry .insert (0 , sounds_path )
251284 else :
@@ -254,7 +287,7 @@ def get_settings():
254287 get = False
255288 def get_message ():
256289 global sounds_path , LANGUAGE_LIST
257- global get , language
290+ global get , language , theme
258291 get = True
259292 from tkinter import filedialog
260293 f_path = filedialog .askopenfilename (title = 'Open Sounds File' , filetypes = [('Sounds' , '*.mp3 *.wav' )])
@@ -264,11 +297,16 @@ def get_message():
264297 path_entry .insert (0 , f_path )
265298
266299 old_language = language
300+ old_theme = theme
301+ new_theme = tk .StringVar ()
302+ new_theme .set (theme )
267303 newlanguage = tk .StringVar ()
268304 newlanguage .set (old_language )
269305 def ok ():
270- global sounds_path , language
306+ global sounds_path , language , theme
271307 language = newlanguage .get ()
308+ theme = new_theme .get ()
309+ update_theme ()
272310 if not get :
273311 f_path = path_entry .get ()
274312 if f_path != '' :
@@ -281,17 +319,20 @@ def ok():
281319 else :
282320 sounds_path = f_path
283321
284- if old_language != language :
322+ if old_language != language or old_theme != theme :
285323 root .destroy ()
286324 get_new_main_root ()
287325
288326 else :
289327 settings_windows .destroy ()
290328
291- path_button = tk .Button (settings_windows , text = '. . .' , command = get_message )
292- ok_button = tk .Button (settings_windows , width = 10 , text = LANGUAGE_LIST [language ]['ok' ], command = ok )
329+ path_button = tk .Button (settings_windows , text = '. . .' , command = get_message , bg = theme , fg = theme_color ,
330+ activebackground = theme , activeforeground = theme )
331+ ok_button = tk .Button (settings_windows , width = 10 , text = LANGUAGE_LIST [language ]['ok' ], command = ok , bg = theme ,
332+ fg = theme_color , activebackground = theme , activeforeground = theme )
293333 no_button = tk .Button (settings_windows , width = 10 , text = LANGUAGE_LIST [language ]['calc' ],
294- command = settings_windows .destroy )
334+ command = settings_windows .destroy , bg = theme , fg = theme_color , activebackground = theme ,
335+ activeforeground = theme )
295336 path_button .place (x = 370 , y = 25 )
296337 ok_button .place (x = 250 , y = 330 )
297338 no_button .place (x = 320 , y = 330 )
@@ -303,21 +344,37 @@ def reset():
303344 path_entry .delete (0 , tk .END )
304345 path_entry .insert (0 , s_path )
305346
306- exit_button = tk .Button (settings_windows , text = LANGUAGE_LIST [language ]['exit' ], command = _exit )
347+ exit_button = tk .Button (settings_windows , text = LANGUAGE_LIST [language ]['exit' ], command = _exit , bg = theme ,
348+ fg = theme_color , activebackground = theme , activeforeground = theme )
307349 exit_button .place (x = 10 , y = 330 )
308350
309- reset_button = tk .Button (settings_windows , text = LANGUAGE_LIST [language ]['reset' ], command = reset )
351+ reset_button = tk .Button (settings_windows , text = LANGUAGE_LIST [language ]['reset' ], command = reset , bg = theme ,
352+ fg = theme_color , activebackground = theme , activeforeground = theme )
310353 reset_button .place (x = 10 , y = 55 )
311354
312355
313- la_text = tk .Label (settings_windows , text = LANGUAGE_LIST [language ]['language text' ])
356+ la_text = tk .Label (settings_windows , text = LANGUAGE_LIST [language ]['language text' ], bg = theme , fg = theme_color )
314357 la_text .place (x = 5 , y = 100 )
315- chinese_radiobutton = tk .Radiobutton (settings_windows , text = '简体中文' , variable = newlanguage , value = 'Chinese' )
316- english_radiobutton = tk .Radiobutton (settings_windows , text = 'English(USA)' , variable = newlanguage , value = 'English' )
358+ chinese_radiobutton = tk .Radiobutton (settings_windows , text = '简体中文' , variable = newlanguage , value = 'Chinese' ,
359+ bg = theme , fg = theme_color , activebackground = theme , activeforeground = theme ,
360+ selectcolor = theme )
361+ english_radiobutton = tk .Radiobutton (settings_windows , text = 'English(USA)' , variable = newlanguage ,
362+ value = 'English' , bg = theme , fg = theme_color , activebackground = theme ,
363+ activeforeground = theme , selectcolor = theme )
317364 chinese_radiobutton .place (x = 50 , y = 130 )
318365 english_radiobutton .place (x = 50 , y = 160 )
319366
367+ la_text = tk .Label (settings_windows , text = LANGUAGE_LIST [language ]['mode title' ], bg = theme , fg = theme_color )
368+ la_text .place (x = 5 , y = 190 )
320369
370+ black_radiobutton = tk .Radiobutton (settings_windows , text = LANGUAGE_LIST [language ]['black mode' ],
371+ variable = new_theme , value = 'black' , bg = theme , fg = theme_color ,
372+ activebackground = theme , activeforeground = theme_color , selectcolor = theme )
373+ white_radiobutton = tk .Radiobutton (settings_windows , text = LANGUAGE_LIST [language ]['white mode' ],
374+ variable = new_theme , value = 'white' , bg = theme , fg = theme_color ,
375+ activebackground = theme , activeforeground = theme_color , selectcolor = theme )
376+ black_radiobutton .place (x = 50 , y = 220 )
377+ white_radiobutton .place (x = 50 , y = 250 )
321378
322379 settings_windows .update ()
323380 settings_windows .mainloop ()
@@ -326,6 +383,7 @@ def reset():
326383def get_about ():
327384 global root , language , LANGUAGE_LIST
328385 about_windows = tk .Toplevel (root )
386+ about_windows .configure (bg = theme )
329387 about_windows .title (LANGUAGE_LIST [language ]['about title' ])
330388 about_windows .wm_attributes ('-alpha' , 0.8 )
331389 about_windows .wm_attributes ('-topmost' , True )
@@ -334,12 +392,12 @@ def get_about():
334392
335393 about_text = LANGUAGE_LIST [language ]['about' ]
336394
337- text_label = tk .Label (about_windows , text = about_text , justify = tk .CENTER )
395+ text_label = tk .Label (about_windows , text = about_text , justify = tk .CENTER , bg = theme , fg = theme_color )
338396 text_label .pack ()
339397
340398 support_image = tk .PhotoImage (file = r'.\images\support.png' )
341399 support_image = support_image .subsample (2 , 2 )
342- support_label = tk .Label (about_windows , image = support_image )
400+ support_label = tk .Label (about_windows , image = support_image , bg = theme , fg = theme_color )
343401 support_label .pack ()
344402
345403 about_windows .mainloop ()
0 commit comments