2022-03-05 04:01:07 +03:00
|
|
|
import tkinter
|
|
|
|
import sys
|
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
from .ctk_canvas import CTkCanvas
|
2022-04-09 22:45:57 +03:00
|
|
|
from ..customtkinter_theme_manager import CTkThemeManager
|
|
|
|
from ..customtkinter_settings import CTkSettings
|
|
|
|
from ..customtkinter_draw_engine import CTkDrawEngine
|
2022-04-20 23:50:57 +03:00
|
|
|
from .widget_base_class import CTkBaseClass
|
2022-03-05 04:01:07 +03:00
|
|
|
|
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
class CTkRadioButton(CTkBaseClass):
|
2022-03-05 04:01:07 +03:00
|
|
|
def __init__(self, *args,
|
|
|
|
bg_color=None,
|
|
|
|
fg_color="default_theme",
|
|
|
|
hover_color="default_theme",
|
|
|
|
border_color="default_theme",
|
|
|
|
border_width_unchecked="default_theme",
|
|
|
|
border_width_checked="default_theme",
|
|
|
|
width=22,
|
|
|
|
height=22,
|
|
|
|
corner_radius="default_theme",
|
|
|
|
text_font="default_theme",
|
|
|
|
text_color="default_theme",
|
|
|
|
text="CTkRadioButton",
|
2022-03-10 04:12:30 +03:00
|
|
|
text_color_disabled="default_theme",
|
2022-03-05 04:01:07 +03:00
|
|
|
hover=True,
|
|
|
|
command=None,
|
|
|
|
state=tkinter.NORMAL,
|
|
|
|
value=0,
|
|
|
|
variable=None,
|
|
|
|
textvariable=None,
|
|
|
|
**kwargs):
|
2022-04-20 23:50:57 +03:00
|
|
|
|
|
|
|
# transfer basic functionality (bg_color, size, appearance_mode, scaling) to CTkBaseClass
|
|
|
|
super().__init__(*args, bg_color=bg_color, width=width, height=height, **kwargs)
|
|
|
|
|
|
|
|
# color
|
2022-03-05 04:01:07 +03:00
|
|
|
self.fg_color = CTkThemeManager.theme["color"]["button"] if fg_color == "default_theme" else fg_color
|
|
|
|
self.hover_color = CTkThemeManager.theme["color"]["button_hover"] if hover_color == "default_theme" else hover_color
|
|
|
|
self.border_color = CTkThemeManager.theme["color"]["checkbox_border"] if border_color == "default_theme" else border_color
|
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
# shape
|
2022-03-05 04:01:07 +03:00
|
|
|
self.corner_radius = CTkThemeManager.theme["shape"]["radiobutton_corner_radius"] if corner_radius == "default_theme" else corner_radius
|
|
|
|
self.border_width_unchecked = CTkThemeManager.theme["shape"]["radiobutton_border_width_unchecked"] if border_width_unchecked == "default_theme" else border_width_unchecked
|
|
|
|
self.border_width_checked = CTkThemeManager.theme["shape"]["radiobutton_border_width_checked"] if border_width_checked == "default_theme" else border_width_checked
|
|
|
|
self.border_width = self.border_width_unchecked
|
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
# text
|
2022-03-05 04:01:07 +03:00
|
|
|
self.text = text
|
2022-04-20 23:50:57 +03:00
|
|
|
self.text_label = None
|
2022-03-05 04:01:07 +03:00
|
|
|
self.text_color = CTkThemeManager.theme["color"]["text"] if text_color == "default_theme" else text_color
|
2022-03-10 04:12:30 +03:00
|
|
|
self.text_color_disabled = CTkThemeManager.theme["color"]["text_disabled"] if text_color_disabled == "default_theme" else text_color_disabled
|
2022-03-05 04:01:07 +03:00
|
|
|
self.text_font = (CTkThemeManager.theme["text"]["font"], CTkThemeManager.theme["text"]["size"]) if text_font == "default_theme" else text_font
|
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
# callback and control variables
|
2022-03-05 04:01:07 +03:00
|
|
|
self.function = command
|
|
|
|
self.state = state
|
|
|
|
self.hover = hover
|
|
|
|
self.check_state = False
|
|
|
|
self.value = value
|
|
|
|
self.variable: tkinter.Variable = variable
|
|
|
|
self.variable_callback_blocked = False
|
|
|
|
self.textvariable = textvariable
|
|
|
|
self.variable_callback_name = None
|
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
# configure grid system (3x1)
|
2022-03-05 15:05:39 +03:00
|
|
|
self.grid_columnconfigure(0, weight=0)
|
|
|
|
self.grid_columnconfigure(1, weight=0, minsize=6)
|
|
|
|
self.grid_columnconfigure(2, weight=1)
|
|
|
|
|
2022-03-05 04:01:07 +03:00
|
|
|
self.canvas = CTkCanvas(master=self,
|
|
|
|
highlightthickness=0,
|
|
|
|
width=self.width,
|
|
|
|
height=self.height)
|
2022-03-05 15:05:39 +03:00
|
|
|
self.canvas.grid(row=0, column=0, padx=0, pady=0, columnspan=1)
|
2022-03-05 04:01:07 +03:00
|
|
|
self.draw_engine = CTkDrawEngine(self.canvas, CTkSettings.preferred_drawing_method)
|
|
|
|
|
2022-03-10 04:12:30 +03:00
|
|
|
self.canvas.bind("<Enter>", self.on_enter)
|
|
|
|
self.canvas.bind("<Leave>", self.on_leave)
|
2022-03-05 04:01:07 +03:00
|
|
|
self.canvas.bind("<Button-1>", self.invoke)
|
|
|
|
self.canvas.bind("<Button-1>", self.invoke)
|
|
|
|
|
2022-03-10 04:12:30 +03:00
|
|
|
self.set_cursor()
|
2022-03-05 04:01:07 +03:00
|
|
|
self.draw() # initial draw
|
|
|
|
|
|
|
|
if self.variable is not None:
|
|
|
|
self.variable_callback_name = self.variable.trace_add("write", self.variable_callback)
|
|
|
|
if self.variable.get() == self.value:
|
|
|
|
self.select(from_variable_callback=True)
|
|
|
|
else:
|
|
|
|
self.deselect(from_variable_callback=True)
|
|
|
|
|
|
|
|
def destroy(self):
|
|
|
|
if self.variable is not None:
|
|
|
|
self.variable.trace_remove("write", self.variable_callback_name)
|
|
|
|
|
|
|
|
super().destroy()
|
|
|
|
|
2022-04-20 23:50:57 +03:00
|
|
|
def draw(self, no_color_updates=False):
|
2022-03-05 04:01:07 +03:00
|
|
|
requires_recoloring = self.draw_engine.draw_rounded_rect_with_border(self.width, self.height, self.corner_radius, self.border_width)
|
|
|
|
|
|
|
|
self.canvas.configure(bg=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
|
|
|
|
self.configure(bg=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
|
|
|
|
|
|
|
|
if self.check_state is False:
|
|
|
|
self.canvas.itemconfig("border_parts",
|
|
|
|
outline=CTkThemeManager.single_color(self.border_color, self.appearance_mode),
|
|
|
|
fill=CTkThemeManager.single_color(self.border_color, self.appearance_mode))
|
|
|
|
else:
|
|
|
|
self.canvas.itemconfig("border_parts",
|
|
|
|
outline=CTkThemeManager.single_color(self.fg_color, self.appearance_mode),
|
|
|
|
fill=CTkThemeManager.single_color(self.fg_color, self.appearance_mode))
|
|
|
|
|
|
|
|
self.canvas.itemconfig("inner_parts",
|
|
|
|
outline=CTkThemeManager.single_color(self.bg_color, self.appearance_mode),
|
|
|
|
fill=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
|
|
|
|
|
|
|
|
if self.text_label is None:
|
|
|
|
self.text_label = tkinter.Label(master=self,
|
2022-03-05 15:05:39 +03:00
|
|
|
bd=0,
|
2022-03-05 04:01:07 +03:00
|
|
|
text=self.text,
|
|
|
|
justify=tkinter.LEFT,
|
|
|
|
font=self.text_font)
|
2022-03-05 15:05:39 +03:00
|
|
|
self.text_label.grid(row=0, column=2, padx=0, pady=0, sticky="w")
|
2022-03-05 04:01:07 +03:00
|
|
|
self.text_label["anchor"] = "w"
|
|
|
|
|
2022-03-10 04:12:30 +03:00
|
|
|
if self.state == tkinter.DISABLED:
|
|
|
|
self.text_label.configure(fg=CTkThemeManager.single_color(self.text_color_disabled, self.appearance_mode))
|
|
|
|
else:
|
|
|
|
self.text_label.configure(fg=CTkThemeManager.single_color(self.text_color, self.appearance_mode))
|
|
|
|
|
2022-03-05 04:01:07 +03:00
|
|
|
self.text_label.configure(bg=CTkThemeManager.single_color(self.bg_color, self.appearance_mode))
|
|
|
|
|
|
|
|
self.set_text(self.text)
|
|
|
|
|
|
|
|
def configure(self, *args, **kwargs):
|
|
|
|
require_redraw = False # some attribute changes require a call of self.draw()
|
|
|
|
|
|
|
|
if "text" in kwargs:
|
|
|
|
self.set_text(kwargs["text"])
|
|
|
|
del kwargs["text"]
|
|
|
|
|
|
|
|
if "state" in kwargs:
|
2022-03-10 04:12:30 +03:00
|
|
|
self.state = kwargs["state"]
|
|
|
|
self.set_cursor()
|
|
|
|
require_redraw = True
|
2022-03-05 04:01:07 +03:00
|
|
|
del kwargs["state"]
|
|
|
|
|
|
|
|
if "fg_color" in kwargs:
|
|
|
|
self.fg_color = kwargs["fg_color"]
|
|
|
|
require_redraw = True
|
|
|
|
del kwargs["fg_color"]
|
|
|
|
|
|
|
|
if "bg_color" in kwargs:
|
|
|
|
if kwargs["bg_color"] is None:
|
|
|
|
self.bg_color = self.detect_color_of_master()
|
|
|
|
else:
|
|
|
|
self.bg_color = kwargs["bg_color"]
|
|
|
|
require_redraw = True
|
|
|
|
del kwargs["bg_color"]
|
|
|
|
|
|
|
|
if "hover_color" in kwargs:
|
|
|
|
self.hover_color = kwargs["hover_color"]
|
|
|
|
require_redraw = True
|
|
|
|
del kwargs["hover_color"]
|
|
|
|
|
|
|
|
if "text_color" in kwargs:
|
|
|
|
self.text_color = kwargs["text_color"]
|
|
|
|
require_redraw = True
|
|
|
|
del kwargs["text_color"]
|
|
|
|
|
|
|
|
if "border_color" in kwargs:
|
|
|
|
self.border_color = kwargs["border_color"]
|
|
|
|
require_redraw = True
|
|
|
|
del kwargs["border_color"]
|
|
|
|
|
|
|
|
if "border_width" in kwargs:
|
|
|
|
self.border_width = kwargs["border_width"]
|
|
|
|
require_redraw = True
|
|
|
|
del kwargs["border_width"]
|
|
|
|
|
|
|
|
if "command" in kwargs:
|
|
|
|
self.function = kwargs["command"]
|
|
|
|
del kwargs["command"]
|
|
|
|
|
|
|
|
if "variable" in kwargs:
|
|
|
|
if self.variable is not None:
|
|
|
|
self.variable.trace_remove("write", self.variable_callback_name)
|
|
|
|
|
|
|
|
self.variable = kwargs["variable"]
|
|
|
|
|
|
|
|
if self.variable is not None and self.variable != "":
|
|
|
|
self.variable_callback_name = self.variable.trace_add("write", self.variable_callback)
|
|
|
|
if self.variable.get() == self.value:
|
|
|
|
self.select(from_variable_callback=True)
|
|
|
|
else:
|
|
|
|
self.deselect(from_variable_callback=True)
|
|
|
|
else:
|
|
|
|
self.variable = None
|
|
|
|
|
|
|
|
del kwargs["variable"]
|
|
|
|
|
|
|
|
super().configure(*args, **kwargs)
|
|
|
|
|
|
|
|
if require_redraw:
|
|
|
|
self.draw()
|
|
|
|
|
2022-03-10 04:12:30 +03:00
|
|
|
def set_cursor(self):
|
2022-03-05 04:01:07 +03:00
|
|
|
if self.state == tkinter.DISABLED:
|
|
|
|
if sys.platform == "darwin" and CTkSettings.hand_cursor_enabled:
|
|
|
|
self.canvas.configure(cursor="arrow")
|
2022-03-10 04:12:30 +03:00
|
|
|
elif sys.platform.startswith("win") and CTkSettings.hand_cursor_enabled:
|
2022-03-05 04:01:07 +03:00
|
|
|
self.canvas.configure(cursor="arrow")
|
|
|
|
|
|
|
|
elif self.state == tkinter.NORMAL:
|
|
|
|
if sys.platform == "darwin" and CTkSettings.hand_cursor_enabled:
|
|
|
|
self.canvas.configure(cursor="pointinghand")
|
2022-03-10 04:12:30 +03:00
|
|
|
elif sys.platform.startswith("win") and CTkSettings.hand_cursor_enabled:
|
2022-03-05 04:01:07 +03:00
|
|
|
self.canvas.configure(cursor="hand2")
|
|
|
|
|
|
|
|
def set_text(self, text):
|
|
|
|
self.text = text
|
|
|
|
if self.text_label is not None:
|
2022-03-05 15:05:39 +03:00
|
|
|
self.text_label.configure(text=self.text)
|
2022-03-05 04:01:07 +03:00
|
|
|
else:
|
2022-03-05 15:05:39 +03:00
|
|
|
sys.stderr.write("ERROR (CTkButton): Cant change text because radiobutton has no text.")
|
2022-03-05 04:01:07 +03:00
|
|
|
|
|
|
|
def on_enter(self, event=0):
|
2022-03-10 04:12:30 +03:00
|
|
|
if self.hover is True and self.state == tkinter.NORMAL:
|
2022-03-05 04:01:07 +03:00
|
|
|
self.canvas.itemconfig("border_parts",
|
|
|
|
fill=CTkThemeManager.single_color(self.hover_color, self.appearance_mode),
|
|
|
|
outline=CTkThemeManager.single_color(self.hover_color, self.appearance_mode))
|
|
|
|
|
|
|
|
def on_leave(self, event=0):
|
|
|
|
if self.hover is True:
|
|
|
|
if self.check_state is True:
|
|
|
|
self.canvas.itemconfig("border_parts",
|
|
|
|
fill=CTkThemeManager.single_color(self.fg_color, self.appearance_mode),
|
|
|
|
outline=CTkThemeManager.single_color(self.fg_color, self.appearance_mode))
|
|
|
|
else:
|
|
|
|
self.canvas.itemconfig("border_parts",
|
|
|
|
fill=CTkThemeManager.single_color(self.border_color, self.appearance_mode),
|
|
|
|
outline=CTkThemeManager.single_color(self.border_color, self.appearance_mode))
|
|
|
|
|
|
|
|
def variable_callback(self, var_name, index, mode):
|
|
|
|
if not self.variable_callback_blocked:
|
|
|
|
if self.variable.get() == self.value:
|
|
|
|
self.select(from_variable_callback=True)
|
|
|
|
else:
|
|
|
|
self.deselect(from_variable_callback=True)
|
|
|
|
|
|
|
|
def invoke(self, event=0):
|
|
|
|
if self.function is not None:
|
|
|
|
self.function()
|
|
|
|
|
|
|
|
if self.state == tkinter.NORMAL:
|
|
|
|
if self.check_state is False:
|
|
|
|
self.check_state = True
|
|
|
|
self.select()
|
|
|
|
|
|
|
|
def select(self, from_variable_callback=False):
|
|
|
|
self.check_state = True
|
|
|
|
self.border_width = self.border_width_checked
|
|
|
|
self.draw()
|
|
|
|
|
|
|
|
if self.variable is not None and not from_variable_callback:
|
|
|
|
self.variable_callback_blocked = True
|
|
|
|
self.variable.set(self.value)
|
|
|
|
self.variable_callback_blocked = False
|
|
|
|
|
|
|
|
def deselect(self, from_variable_callback=False):
|
|
|
|
self.check_state = False
|
|
|
|
self.border_width = self.border_width_unchecked
|
|
|
|
self.draw()
|
|
|
|
|
|
|
|
if self.variable is not None and not from_variable_callback:
|
|
|
|
self.variable_callback_blocked = True
|
|
|
|
self.variable.set("")
|
|
|
|
self.variable_callback_blocked = False
|