@@ -42,39 +42,36 @@ class SettingsWindow(Dialog):
4242
4343 def body (self , master ):
4444 ttk .Label (master , text = 'Select host API:' ).pack (anchor = 'w' )
45- hostapi_list = ttk .Combobox (master , state = 'readonly' , width = 50 )
46- hostapi_list .pack ()
47- hostapi_list ['values' ] = [hostapi [ 'name' ]
48- for hostapi in sd .query_hostapis ()]
45+ self . hostapi_list = ttk .Combobox (master , state = 'readonly' , width = 50 )
46+ self . hostapi_list .pack ()
47+ self . hostapi_list ['values' ] = [
48+ hostapi [ 'name' ] for hostapi in sd .query_hostapis ()]
4949
5050 ttk .Label (master , text = 'Select sound device:' ).pack (anchor = 'w' )
51- device_ids = []
52- device_list = ttk .Combobox (master , state = 'readonly' , width = 50 )
53- device_list .pack ()
54-
55- def update_device_list (* args ):
56- hostapi = sd .query_hostapis (hostapi_list .current ())
57- nonlocal device_ids
58- device_ids = [
59- idx
60- for idx in hostapi ['devices' ]
61- if sd .query_devices (idx )['max_input_channels' ] > 0 ]
62- device_list ['values' ] = [
63- sd .query_devices (idx )['name' ] for idx in device_ids ]
64- default = hostapi ['default_input_device' ]
65- if default >= 0 :
66- device_list .current (device_ids .index (default ))
67- device_list .event_generate ('<<ComboboxSelected>>' )
68-
69- def select_device (* args ):
70- self .result = device_ids [device_list .current ()]
71-
72- hostapi_list .bind ('<<ComboboxSelected>>' , update_device_list )
73- device_list .bind ('<<ComboboxSelected>>' , select_device )
51+ self .device_ids = []
52+ self .device_list = ttk .Combobox (master , state = 'readonly' , width = 50 )
53+ self .device_list .pack ()
7454
55+ self .hostapi_list .bind ('<<ComboboxSelected>>' , self .update_device_list )
7556 with contextlib .suppress (sd .PortAudioError ):
76- hostapi_list .current (sd .default .hostapi )
77- hostapi_list .event_generate ('<<ComboboxSelected>>' )
57+ self .hostapi_list .current (sd .default .hostapi )
58+ self .hostapi_list .event_generate ('<<ComboboxSelected>>' )
59+
60+ def update_device_list (self , * args ):
61+ hostapi = sd .query_hostapis (self .hostapi_list .current ())
62+ self .device_ids = [
63+ idx
64+ for idx in hostapi ['devices' ]
65+ if sd .query_devices (idx )['max_input_channels' ] > 0 ]
66+ self .device_list ['values' ] = [
67+ sd .query_devices (idx )['name' ] for idx in self .device_ids ]
68+ default = hostapi ['default_input_device' ]
69+ if default >= 0 :
70+ self .device_list .current (self .device_ids .index (default ))
71+
72+ def validate (self ):
73+ self .result = self .device_ids [self .device_list .current ()]
74+ return True
7875
7976
8077class RecGui (tk .Tk ):
@@ -203,7 +200,8 @@ def _wait_for_thread(self):
203200
204201 def on_settings (self , * args ):
205202 w = SettingsWindow (self , 'Settings' )
206- self .create_stream (device = w .result )
203+ if w .result is not None :
204+ self .create_stream (device = w .result )
207205
208206 def init_buttons (self ):
209207 self .rec_button ['text' ] = 'record'
0 commit comments