[bugfix] CTkEntry系で、入力値に簡易バリデーション追加。

TkinterのVariableを使っているCTkEntryは、型を可能なものはStrings型に。(空文字などint型に変換できずエラーになってしまうので)
UI側ではどんな型であれ値であれコントローラへ値を渡し、コントローラが判断して処理するように(CTkEntryにセットしたり値を削除したり)
This commit is contained in:
Sakamoto Shiina
2023-09-12 23:24:05 +09:00
parent f071be5cb6
commit 60e73760a7
5 changed files with 146 additions and 37 deletions

78
main.py
View File

@@ -293,8 +293,21 @@ def callbackSetMicDevice(value):
view.replaceConfigWindowMicThresholdCheckButtonToPassive()
def callbackSetMicEnergyThreshold(value):
print("callbackSetMicEnergyThreshold", int(value))
print("callbackSetMicEnergyThreshold", value)
try:
if 0 > int(value) or int(value) > config.MAX_MIC_ENERGY_THRESHOLD: raise ValueError()
except:
view.setGuiVariable_MicEnergyThreshold(
slider_value=config.INPUT_MIC_ENERGY_THRESHOLD,
entry_value=None,
)
return
config.INPUT_MIC_ENERGY_THRESHOLD = int(value)
view.setGuiVariable_MicEnergyThreshold(
slider_value=config.INPUT_MIC_ENERGY_THRESHOLD,
entry_value=str(config.INPUT_MIC_ENERGY_THRESHOLD),
)
def callbackSetMicDynamicEnergyThreshold(value):
print("callbackSetMicDynamicEnergyThreshold", value)
@@ -321,16 +334,34 @@ def callbackCheckMicThreshold(is_turned_on):
# view.setConfigWindowCompactModeSwitchStatusToNormal()
def callbackSetMicRecordTimeout(value):
print("callbackSetMicRecordTimeout", int(value))
print("callbackSetMicRecordTimeout", value)
try:
if int(value) < 0: raise ValueError()
except:
view.setGuiVariable_MicRecordTimeout(delete=True)
return
config.INPUT_MIC_RECORD_TIMEOUT = int(value)
view.setGuiVariable_MicRecordTimeout(str(config.INPUT_MIC_RECORD_TIMEOUT))
def callbackSetMicPhraseTimeout(value):
print("callbackSetMicPhraseTimeout", int(value))
print("callbackSetMicPhraseTimeout", value)
try:
if int(value) < 0: raise ValueError()
except:
view.setGuiVariable_MicPhraseTimeout(delete=True)
return
config.INPUT_MIC_PHRASE_TIMEOUT = int(value)
view.setGuiVariable_MicPhraseTimeout(str(config.INPUT_MIC_PHRASE_TIMEOUT))
def callbackSetMicMaxPhrases(value):
print("callbackSetMicMaxPhrases", int(value))
print("callbackSetMicMaxPhrases", value)
try:
if int(value) < 0: raise ValueError()
except:
view.setGuiVariable_MicMaxPhrases(delete=True)
return
config.INPUT_MIC_MAX_PHRASES = int(value)
view.setGuiVariable_MicMaxPhrases(str(config.INPUT_MIC_MAX_PHRASES))
def callbackSetMicWordFilter(value):
print("callbackSetMicWordFilter", value)
@@ -354,8 +385,21 @@ def callbackSetSpeakerDevice(value):
view.replaceConfigWindowSpeakerThresholdCheckButtonToPassive()
def callbackSetSpeakerEnergyThreshold(value):
print("callbackSetSpeakerEnergyThreshold", int(value))
print("callbackSetSpeakerEnergyThreshold", value)
try:
if 0 > int(value) or int(value) > config.MAX_SPEAKER_ENERGY_THRESHOLD: raise ValueError()
except:
view.setGuiVariable_SpeakerEnergyThreshold(
slider_value=config.INPUT_SPEAKER_ENERGY_THRESHOLD,
entry_value=None,
)
return
config.INPUT_SPEAKER_ENERGY_THRESHOLD = int(value)
view.setGuiVariable_SpeakerEnergyThreshold(
slider_value=config.INPUT_SPEAKER_ENERGY_THRESHOLD,
entry_value=str(config.INPUT_SPEAKER_ENERGY_THRESHOLD),
)
def callbackSetSpeakerDynamicEnergyThreshold(value):
print("callbackSetSpeakerDynamicEnergyThreshold", value)
@@ -383,16 +427,34 @@ def callbackCheckSpeakerThreshold(is_turned_on):
# view.setConfigWindowCompactModeSwitchStatusToNormal()
def callbackSetSpeakerRecordTimeout(value):
print("callbackSetSpeakerRecordTimeout", int(value))
print("callbackSetSpeakerRecordTimeout", value)
try:
if int(value) < 0: raise ValueError()
except:
view.setGuiVariable_SpeakerRecordTimeout(delete=True)
return
config.INPUT_SPEAKER_RECORD_TIMEOUT = int(value)
view.setGuiVariable_SpeakerRecordTimeout(str(config.INPUT_SPEAKER_RECORD_TIMEOUT))
def callbackSetSpeakerPhraseTimeout(value):
print("callbackSetSpeakerPhraseTimeout", int(value))
print("callbackSetSpeakerPhraseTimeout", value)
try:
if int(value) < 0: raise ValueError()
except:
view.setGuiVariable_SpeakerPhraseTimeout(delete=True)
return
config.INPUT_SPEAKER_PHRASE_TIMEOUT = int(value)
view.setGuiVariable_SpeakerPhraseTimeout(str(config.INPUT_SPEAKER_PHRASE_TIMEOUT))
def callbackSetSpeakerMaxPhrases(value):
print("callbackSetSpeakerMaxPhrases", int(value))
print("callbackSetSpeakerMaxPhrases", value)
try:
if int(value) < 0: raise ValueError()
except:
view.setGuiVariable_SpeakerMaxPhrases(delete=True)
return
config.INPUT_SPEAKER_MAX_PHRASES = int(value)
view.setGuiVariable_SpeakerMaxPhrases(str(config.INPUT_SPEAKER_MAX_PHRASES))
# Others Tab

73
view.py
View File

@@ -1,3 +1,4 @@
from typing import Union
from types import SimpleNamespace
from tkinter import font as tk_font
from languages import selectable_languages
@@ -146,7 +147,8 @@ class View():
VAR_DESC_MIC_ENERGY_THRESHOLD=StringVar(value="Slider to modify the threshold for activating voice input.\nPress the microphone button to start input and speak something, so you can adjust it while monitoring the actual volume. 0 to 2000 (Default: 300)"),
SLIDER_RANGE_MIC_ENERGY_THRESHOLD=(0, config.MAX_MIC_ENERGY_THRESHOLD),
CALLBACK_CHECK_MIC_THRESHOLD=None,
VAR_MIC_ENERGY_THRESHOLD=IntVar(value=config.INPUT_MIC_ENERGY_THRESHOLD),
VAR_MIC_ENERGY_THRESHOLD__SLIDER=IntVar(value=config.INPUT_MIC_ENERGY_THRESHOLD),
VAR_MIC_ENERGY_THRESHOLD__ENTRY=StringVar(value=config.INPUT_MIC_ENERGY_THRESHOLD),
VAR_LABEL_MIC_DYNAMIC_ENERGY_THRESHOLD=StringVar(value="Mic Dynamic Energy Threshold"),
VAR_DESC_MIC_DYNAMIC_ENERGY_THRESHOLD=StringVar(value="When this feature is selected, it will automatically adjust in a way that works well, based on the set Mic Energy Threshold."),
@@ -156,17 +158,17 @@ class View():
VAR_LABEL_MIC_RECORD_TIMEOUT=StringVar(value="Mic Record Timeout"),
VAR_DESC_MIC_RECORD_TIMEOUT=StringVar(value="(Default: 3)"),
CALLBACK_SET_MIC_RECORD_TIMEOUT=None,
VAR_MIC_RECORD_TIMEOUT=IntVar(value=config.INPUT_MIC_RECORD_TIMEOUT),
VAR_MIC_RECORD_TIMEOUT=StringVar(value=config.INPUT_MIC_RECORD_TIMEOUT),
VAR_LABEL_MIC_PHRASE_TIMEOUT=StringVar(value="Mic Phrase Timeout"),
VAR_DESC_MIC_PHRASE_TIMEOUT=StringVar(value="(Default: 3)"),
CALLBACK_SET_MIC_PHRASE_TIMEOUT=None,
VAR_MIC_PHRASE_TIMEOUT=IntVar(value=config.INPUT_MIC_PHRASE_TIMEOUT),
VAR_MIC_PHRASE_TIMEOUT=StringVar(value=config.INPUT_MIC_PHRASE_TIMEOUT),
VAR_LABEL_MIC_MAX_PHRASES=StringVar(value="Mic Max Phrases"),
VAR_DESC_MIC_MAX_PHRASES=StringVar(value="It will stop recording and send the recordings when the set count of phrase(s) is reached. (Default: 10)"),
CALLBACK_SET_MIC_MAX_PHRASES=None,
VAR_MIC_MAX_PHRASES=IntVar(value=config.INPUT_MIC_MAX_PHRASES),
VAR_MIC_MAX_PHRASES=StringVar(value=config.INPUT_MIC_MAX_PHRASES),
VAR_LABEL_MIC_WORD_FILTER=StringVar(value="Mic Word Filter"),
VAR_DESC_MIC_WORD_FILTER=StringVar(value="It will not send the sentence if the word(s) included in the set list of words.\nHow to set: e.g. AAA,BBB,CCC"),
@@ -185,7 +187,8 @@ class View():
VAR_DESC_SPEAKER_ENERGY_THRESHOLD=StringVar(value="Slider to modify the threshold for activating voice input.\nPress the headphones mark button to start input and speak something, so you can adjust it while monitoring the actual volume. 0 to 4000 (Default: 300)"),
SLIDER_RANGE_SPEAKER_ENERGY_THRESHOLD=(0, config.MAX_SPEAKER_ENERGY_THRESHOLD),
CALLBACK_CHECK_SPEAKER_THRESHOLD=None,
VAR_SPEAKER_ENERGY_THRESHOLD=IntVar(value=config.INPUT_SPEAKER_ENERGY_THRESHOLD),
VAR_SPEAKER_ENERGY_THRESHOLD__SLIDER=IntVar(value=config.INPUT_SPEAKER_ENERGY_THRESHOLD),
VAR_SPEAKER_ENERGY_THRESHOLD__ENTRY=StringVar(value=config.INPUT_SPEAKER_ENERGY_THRESHOLD),
VAR_LABEL_SPEAKER_DYNAMIC_ENERGY_THRESHOLD=StringVar(value="Speaker Dynamic Energy Threshold"),
VAR_DESC_SPEAKER_DYNAMIC_ENERGY_THRESHOLD=StringVar(value="When this feature is selected, it will automatically adjust in a way that works well, based on the set Speaker Energy Threshold."),
@@ -195,17 +198,17 @@ class View():
VAR_LABEL_SPEAKER_RECORD_TIMEOUT=StringVar(value="Speaker Record Timeout"),
VAR_DESC_SPEAKER_RECORD_TIMEOUT=StringVar(value="(Default: 3)"),
CALLBACK_SET_SPEAKER_RECORD_TIMEOUT=None,
VAR_SPEAKER_RECORD_TIMEOUT=IntVar(value=config.INPUT_SPEAKER_RECORD_TIMEOUT),
VAR_SPEAKER_RECORD_TIMEOUT=StringVar(value=config.INPUT_SPEAKER_RECORD_TIMEOUT),
VAR_LABEL_SPEAKER_PHRASE_TIMEOUT=StringVar(value="Speaker Phrase Timeout"),
VAR_DESC_SPEAKER_PHRASE_TIMEOUT=StringVar(value="It will stop recording and receive the recordings when the set second(s) is reached. (Default: 3)"),
CALLBACK_SET_SPEAKER_PHRASE_TIMEOUT=None,
VAR_SPEAKER_PHRASE_TIMEOUT=IntVar(value=config.INPUT_SPEAKER_PHRASE_TIMEOUT),
VAR_SPEAKER_PHRASE_TIMEOUT=StringVar(value=config.INPUT_SPEAKER_PHRASE_TIMEOUT),
VAR_LABEL_SPEAKER_MAX_PHRASES=StringVar(value="Speaker Max Phrases"),
VAR_DESC_SPEAKER_MAX_PHRASES=StringVar(value="It will stop recording and receive the recordings when the set count of phrase(s) is reached. (Default: 10)"),
CALLBACK_SET_SPEAKER_MAX_PHRASES=None,
VAR_SPEAKER_MAX_PHRASES=IntVar(value=config.INPUT_SPEAKER_MAX_PHRASES),
VAR_SPEAKER_MAX_PHRASES=StringVar(value=config.INPUT_SPEAKER_MAX_PHRASES),
# Others Tab
@@ -240,7 +243,7 @@ class View():
VAR_LABEL_OSC_PORT=StringVar(value="OSC Port"),
VAR_DESC_OSC_PORT=StringVar(value="(Default: 9000)"),
CALLBACK_SET_OSC_PORT=None,
VAR_OSC_PORT=IntVar(value=config.OSC_PORT),
VAR_OSC_PORT=StringVar(value=config.OSC_PORT),
)
@@ -555,4 +558,56 @@ class View():
def updateSetProgressBar_SpeakerEnergy(self, new_speaker_energy):
vrct_gui.config_window.sb__progressbar_x_slider__progressbar_speaker_energy_threshold.set(new_speaker_energy/config.MAX_SPEAKER_ENERGY_THRESHOLD)
def setGuiVariable_MicEnergyThreshold(self, slider_value:int, entry_value:Union[None, str]=None):
self.view_variable.VAR_MIC_ENERGY_THRESHOLD__SLIDER.set(slider_value)
if entry_value is None:
self._clearEntryBox(vrct_gui.config_window.sb__progressbar_x_slider__entry_mic_energy_threshold)
else:
self.view_variable.VAR_MIC_ENERGY_THRESHOLD__ENTRY.set(entry_value)
def setGuiVariable_SpeakerEnergyThreshold(self, slider_value:int, entry_value:Union[None, str]=None):
self.view_variable.VAR_SPEAKER_ENERGY_THRESHOLD__SLIDER.set(slider_value)
if entry_value is None:
self._clearEntryBox(vrct_gui.config_window.sb__progressbar_x_slider__entry_speaker_energy_threshold)
else:
self.view_variable.VAR_SPEAKER_ENERGY_THRESHOLD__ENTRY.set(entry_value)
def setGuiVariable_MicRecordTimeout(self, value:str="", delete=False):
if delete is True: self._clearEntryBox(vrct_gui.config_window.sb__entry_mic_record_timeout)
self.view_variable.VAR_MIC_RECORD_TIMEOUT.set(value)
def setGuiVariable_MicPhraseTimeout(self, value:str="", delete=False):
if delete is True: self._clearEntryBox(vrct_gui.config_window.sb__entry_mic_phrase_timeout)
self.view_variable.VAR_MIC_PHRASE_TIMEOUT.set(value)
def setGuiVariable_MicMaxPhrases(self, value:str="", delete=False):
if delete is True: self._clearEntryBox(vrct_gui.config_window.sb__entry_mic_max_phrases)
self.view_variable.VAR_MIC_MAX_PHRASES.set(value)
def setGuiVariable_SpeakerRecordTimeout(self, value:str="", delete=False):
if delete is True: self._clearEntryBox(vrct_gui.config_window.sb__entry_speaker_record_timeout)
self.view_variable.VAR_SPEAKER_RECORD_TIMEOUT.set(value)
def setGuiVariable_SpeakerPhraseTimeout(self, value:str="", delete=False):
if delete is True: self._clearEntryBox(vrct_gui.config_window.sb__entry_speaker_phrase_timeout)
self.view_variable.VAR_SPEAKER_PHRASE_TIMEOUT.set(value)
def setGuiVariable_SpeakerMaxPhrases(self, value:str="", delete=False):
if delete is True: self._clearEntryBox(vrct_gui.config_window.sb__entry_speaker_max_phrases)
self.view_variable.VAR_SPEAKER_MAX_PHRASES.set(value)
@staticmethod
def _clearEntryBox(entry_widget):
entry_widget.delete(0, CTK_END)
view = View()

View File

@@ -186,7 +186,8 @@ class _SettingBoxGenerator():
passive_button_attr_name, passive_button_command,
active_button_attr_name, active_button_command,
button_image_file,
variable,
entry_variable,
slider_variable,
slider_number_of_steps: Union[int, None] = None,
):
@@ -205,28 +206,15 @@ class _SettingBoxGenerator():
BUTTON_PADDING = int(BAR_WIDTH + BAR_PADDING + self.uism.SB__PROGRESSBAR_X_SLIDER__BUTTON_RIGHT_PADX)
def adjusted_command__for_entry_bind__Any_KeyRelease(e):
# try:
# int(e.widget.get())
# except:
# e.widget.delete(0, CTK_END)
# return
# print(int(e.widget.get()))
i = int(e.widget.get())
if i < 0 or i > slider_range[1]:
e.widget.delete(0, CTK_END)
i = max(0, min(int(e.widget.get()), slider_range[1]))
# e.widget.insert(0, i)
command(i)
command(e.widget.get())
def adjusted_command__for_slider(value):
command(int(value))
command(value)
entry_widget = CTkEntry(
setting_box_progressbar_x_slider_frame,
width=ENTRY_WIDTH,
height=self.uism.SB__PROGRESSBAR_X_SLIDER__ENTRY_HEIGHT,
textvariable=variable,
textvariable=entry_variable,
font=CTkFont(family=self.FONT_FAMILY, size=self.uism.SB__ENTRY_FONT_SIZE, weight="normal"),
)
@@ -244,7 +232,7 @@ class _SettingBoxGenerator():
to=slider_range[1],
number_of_steps=slider_number_of_steps,
command=adjusted_command__for_slider,
variable=variable,
variable=slider_variable,
height=self.uism.SB__PROGRESSBAR_X_SLIDER__SLIDER_HEIGHT,
width=BAR_WIDTH,
border_width=0,

View File

@@ -74,12 +74,14 @@ def createSettingBox_Mic(setting_box_wrapper, config_window, settings, view_vari
for_var_label_text=view_variable.VAR_LABEL_MIC_ENERGY_THRESHOLD,
for_var_desc_text=view_variable.VAR_DESC_MIC_ENERGY_THRESHOLD,
command=slider_input_mic_energy_threshold_callback,
variable=view_variable.VAR_MIC_ENERGY_THRESHOLD,
entry_attr_name="sb__progressbar_x_slider__entry_mic_energy_threshold",
entry_variable=view_variable.VAR_MIC_ENERGY_THRESHOLD__ENTRY,
slider_attr_name="progressbar_x_slider__slider_mic_energy_threshold",
slider_range=view_variable.SLIDER_RANGE_MIC_ENERGY_THRESHOLD,
slider_variable=view_variable.VAR_MIC_ENERGY_THRESHOLD__SLIDER,
progressbar_attr_name="sb__progressbar_x_slider__progressbar_mic_energy_threshold",

View File

@@ -55,12 +55,14 @@ def createSettingBox_Speaker(setting_box_wrapper, config_window, settings, view_
for_var_label_text=view_variable.VAR_LABEL_SPEAKER_ENERGY_THRESHOLD,
for_var_desc_text=view_variable.VAR_DESC_SPEAKER_ENERGY_THRESHOLD,
command=slider_input_speaker_energy_threshold_callback,
variable=view_variable.VAR_SPEAKER_ENERGY_THRESHOLD,
entry_variable=view_variable.VAR_SPEAKER_ENERGY_THRESHOLD__ENTRY,
entry_attr_name="sb__progressbar_x_slider__entry_speaker_energy_threshold",
slider_attr_name="progressbar_x_slider__slider_speaker_energy_threshold",
slider_range=view_variable.SLIDER_RANGE_SPEAKER_ENERGY_THRESHOLD,
slider_variable=view_variable.VAR_SPEAKER_ENERGY_THRESHOLD__SLIDER,
progressbar_attr_name="sb__progressbar_x_slider__progressbar_speaker_energy_threshold",