mirror of
https://github.com/TomSchimansky/CustomTkinter.git
synced 2023-08-10 21:13:13 +03:00
Merge pull request #157 from mohsen1365ir/master
Enhanced checkbox functionality
This commit is contained in:
commit
039cb1d17c
@ -49,7 +49,11 @@ class CTkCheckBox(CTkBaseClass):
|
|||||||
self.border_width = ThemeManager.theme["shape"]["checkbox_border_width"] if border_width == "default_theme" else border_width
|
self.border_width = ThemeManager.theme["shape"]["checkbox_border_width"] if border_width == "default_theme" else border_width
|
||||||
|
|
||||||
# text
|
# 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_label: Union[tkinter.Label, None] = None
|
||||||
self.text_color = ThemeManager.theme["color"]["text"] if text_color == "default_theme" else text_color
|
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
|
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.function = command
|
||||||
self.state = state
|
self.state = state
|
||||||
self.hover = hover
|
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.onvalue = onvalue
|
||||||
self.offvalue = offvalue
|
self.offvalue = offvalue
|
||||||
self.variable: tkinter.Variable = variable
|
self.variable: tkinter.Variable = variable
|
||||||
self.variable_callback_blocked = False
|
self.variable_callback_blocked = False
|
||||||
self.textvariable = textvariable
|
self.textvariable: tkinter.Variable = textvariable
|
||||||
self.variable_callback_name = None
|
self.variable_callback_name = None
|
||||||
|
|
||||||
# configure grid system (1x3)
|
# configure grid system (1x3)
|
||||||
@ -106,11 +113,10 @@ class CTkCheckBox(CTkBaseClass):
|
|||||||
# set select state according to variable
|
# set select state according to variable
|
||||||
if self.variable is not None:
|
if self.variable is not None:
|
||||||
self.variable_callback_name = self.variable.trace_add("write", self.variable_callback)
|
self.variable_callback_name = self.variable.trace_add("write", self.variable_callback)
|
||||||
if self.variable.get() == self.onvalue:
|
|
||||||
self.select(from_variable_callback=True)
|
if self.textvariable is not None:
|
||||||
elif self.variable.get() == self.offvalue:
|
self.textvariable_callback_name = self.textvariable.trace_add("write", self.textvariable_callback)
|
||||||
self.deselect(from_variable_callback=True)
|
|
||||||
|
|
||||||
self.draw() # initial draw
|
self.draw() # initial draw
|
||||||
self.set_cursor()
|
self.set_cursor()
|
||||||
|
|
||||||
@ -239,6 +245,20 @@ class CTkCheckBox(CTkBaseClass):
|
|||||||
self.variable = None
|
self.variable = None
|
||||||
|
|
||||||
del kwargs["variable"]
|
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)
|
super().configure(*args, **kwargs)
|
||||||
|
|
||||||
@ -305,6 +325,9 @@ class CTkCheckBox(CTkBaseClass):
|
|||||||
elif self.variable.get() == self.offvalue:
|
elif self.variable.get() == self.offvalue:
|
||||||
self.deselect(from_variable_callback=True)
|
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):
|
def toggle(self, event=0):
|
||||||
if self.state == tkinter.NORMAL:
|
if self.state == tkinter.NORMAL:
|
||||||
if self.check_state is True:
|
if self.check_state is True:
|
||||||
@ -314,14 +337,14 @@ class CTkCheckBox(CTkBaseClass):
|
|||||||
self.check_state = True
|
self.check_state = True
|
||||||
self.draw()
|
self.draw()
|
||||||
|
|
||||||
if self.function is not None:
|
|
||||||
self.function()
|
|
||||||
|
|
||||||
if self.variable is not None:
|
if self.variable is not None:
|
||||||
self.variable_callback_blocked = True
|
self.variable_callback_blocked = True
|
||||||
self.variable.set(self.onvalue if self.check_state is True else self.offvalue)
|
self.variable.set(self.onvalue if self.check_state is True else self.offvalue)
|
||||||
self.variable_callback_blocked = False
|
self.variable_callback_blocked = False
|
||||||
|
|
||||||
|
if self.function is not None:
|
||||||
|
self.function()
|
||||||
|
|
||||||
def select(self, from_variable_callback=False):
|
def select(self, from_variable_callback=False):
|
||||||
self.check_state = True
|
self.check_state = True
|
||||||
self.draw()
|
self.draw()
|
||||||
|
@ -274,8 +274,7 @@ class CTkRadioButton(CTkBaseClass):
|
|||||||
self.select()
|
self.select()
|
||||||
|
|
||||||
if self.function is not None:
|
if self.function is not None:
|
||||||
if self.function is not None:
|
self.function()
|
||||||
self.function()
|
|
||||||
|
|
||||||
def select(self, from_variable_callback=False):
|
def select(self, from_variable_callback=False):
|
||||||
self.check_state = True
|
self.check_state = True
|
||||||
|
@ -159,7 +159,7 @@ class CTkBaseClass(tkinter.Frame):
|
|||||||
elif hasattr(master_widget.master, "master"):
|
elif hasattr(master_widget.master, "master"):
|
||||||
return self.detect_color_of_master(self.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:
|
try:
|
||||||
ttk_style = ttk.Style()
|
ttk_style = ttk.Style()
|
||||||
return ttk_style.lookup(master_widget.winfo_class(), 'background')
|
return ttk_style.lookup(master_widget.winfo_class(), 'background')
|
||||||
|
Loading…
Reference in New Issue
Block a user