Код: Выделить всё
class View(TkFrame):
def __init__(self):
...
self.omn_audio_source = ctk.CTkOptionMenu(
master=self.frm_shared_options,
values=[e.value for e in AudioSource],
)
self.omn_audio_source.grid(row=3, column=0, padx=20, pady=0, sticky=ctk.EW)
def bind_commands(self):
self.omn_audio_source.configure(
command=self.callbacks[CallbacksMain.CHANGE_AUDIO_SOURCE],
)
...
def _on_audio_source_change(self, option):
if option != AudioSource.MIC:
self._toggle_input_path_fields(should_show=True)
self.frm_main_entry.grid()
if option != AudioSource.DIRECTORY:
self.chk_autosave.configure(state=ctk.NORMAL)
self.btn_save.configure(state=ctk.NORMAL)
if option in [AudioSource.FILE, AudioSource.DIRECTORY]:
self.btn_main_action.configure(text="Generate transcription")
self.lbl_input_path.configure(text="Input path")
self.btn_input_path_file_explorer.grid()
if self._audio_source == AudioSource.DIRECTORY:
self.chk_autosave.select()
self._on_autosave_change()
self.chk_autosave.configure(state=ctk.DISABLED)
self.btn_save.configure(state=ctk.DISABLED)
elif option == AudioSource.MIC:
self.btn_main_action.configure(text="Start recording")
self._toggle_input_path_fields(should_show=False)
if self.chk_autosave.get():
self._toggle_output_path_fields(should_show=True)
self.frm_main_entry.grid()
else:
self.frm_main_entry.grid_remove()
elif option == AudioSource.YOUTUBE:
self.btn_main_action.configure(text="Generate transcription")
self.lbl_input_path.configure(text="YouTube video URL")
self.btn_input_path_file_explorer.grid_remove()
def display_input_path(self, path):
self.ent_input_path.configure(textvariable=ctk.StringVar(self, path))
Код: Выделить всё
class Controller:
def __init__(self, transcription, view):
self.view = view
self.transcription = transcription # model
self._add_callbacks()
self.view.bind_commands()
def _add_callbacks(self):
callbacks = {
CallbacksMain.CHANGE_TRANSCRIPTION_LANGUAGE: self._change_audio_source,
}
for key, method in callbacks.items():
self.view.add_callback(key, method)
def _change_audio_source(self):
# The following code was previously in the `_on_audio_source_change` method of the view
audio_source = AudioSource(audio_source_str)
self.transcription.audio_source = audio_source
self.view.display_input_path("")
self._on_config_change(
section=ConfigTranscription.Key.SECTION,
key=ConfigTranscription.Key.AUDIO_SOURCE,
new_value=audio_source,
)
# How do I handle the rest of the view logic (`_on_audio_source_change`)?
Подробнее здесь: https://stackoverflow.com/questions/789 ... -compliant