Merge pull request #157 from mohsen1365ir/master

Enhanced checkbox functionality
This commit is contained in:
Tom Schimansky 2022-07-02 08:16:00 -04:00 committed by GitHub
commit 039cb1d17c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 14 deletions

View File

@ -49,7 +49,11 @@ class CTkCheckBox(CTkBaseClass):
self.border_width = ThemeManager.theme["shape"]["checkbox_border_width"] if border_width == "default_theme" else border_width
# text
self.text = text
if textvariable is None:
self.text = text
else:
self.text = textvariable.get()
self.text_label: Union[tkinter.Label, None] = None
self.text_color = ThemeManager.theme["color"]["text"] if text_color == "default_theme" else text_color
self.text_color_disabled = ThemeManager.theme["color"]["text_disabled"] if text_color_disabled == "default_theme" else text_color_disabled
@ -59,12 +63,15 @@ class CTkCheckBox(CTkBaseClass):
self.function = command
self.state = state
self.hover = hover
self.check_state = False
if variable == None:
self.check_state = False
else:
self.check_state = (variable.get() == onvalue)
self.onvalue = onvalue
self.offvalue = offvalue
self.variable: tkinter.Variable = variable
self.variable_callback_blocked = False
self.textvariable = textvariable
self.textvariable: tkinter.Variable = textvariable
self.variable_callback_name = None
# configure grid system (1x3)
@ -106,11 +113,10 @@ class CTkCheckBox(CTkBaseClass):
# set select state according to variable
if self.variable is not None:
self.variable_callback_name = self.variable.trace_add("write", self.variable_callback)
if self.variable.get() == self.onvalue:
self.select(from_variable_callback=True)
elif self.variable.get() == self.offvalue:
self.deselect(from_variable_callback=True)
if self.textvariable is not None:
self.textvariable_callback_name = self.textvariable.trace_add("write", self.textvariable_callback)
self.draw() # initial draw
self.set_cursor()
@ -239,6 +245,20 @@ class CTkCheckBox(CTkBaseClass):
self.variable = None
del kwargs["variable"]
if "textvariable" in kwargs:
if self.textvariable is not None:
self.textvariable.trace_remove("write", self.textvariable_callback_name)
self.textvariable = kwargs["textvariable"]
if self.textvariable is not None and self.textvariable != "":
self.textvariable_callback_name = self.textvariable.trace_add("write", self.textvariable_callback)
self.set_text(self.textvariable.get())
else:
self.textvariable = None
del kwargs["textvariable"]
super().configure(*args, **kwargs)
@ -305,6 +325,9 @@ class CTkCheckBox(CTkBaseClass):
elif self.variable.get() == self.offvalue:
self.deselect(from_variable_callback=True)
def textvariable_callback(self, var_name, index, mode):
self.set_text(self.textvariable.get())
def toggle(self, event=0):
if self.state == tkinter.NORMAL:
if self.check_state is True:
@ -314,14 +337,14 @@ class CTkCheckBox(CTkBaseClass):
self.check_state = True
self.draw()
if self.function is not None:
self.function()
if self.variable is not None:
self.variable_callback_blocked = True
self.variable.set(self.onvalue if self.check_state is True else self.offvalue)
self.variable_callback_blocked = False
if self.function is not None:
self.function()
def select(self, from_variable_callback=False):
self.check_state = True
self.draw()

View File

@ -274,8 +274,7 @@ class CTkRadioButton(CTkBaseClass):
self.select()
if self.function is not None:
if self.function is not None:
self.function()
self.function()
def select(self, from_variable_callback=False):
self.check_state = True

View File

@ -159,7 +159,7 @@ class CTkBaseClass(tkinter.Frame):
elif hasattr(master_widget.master, "master"):
return self.detect_color_of_master(self.master.master)
elif isinstance(master_widget, (ttk.Frame, ttk.LabelFrame, ttk.Notebook)): # master is ttk widget
elif isinstance(master_widget, (ttk.Frame, ttk.LabelFrame, ttk.Notebook, ttk.Label)): # master is ttk widget
try:
ttk_style = ttk.Style()
return ttk_style.lookup(master_widget.winfo_class(), 'background')