mirror of
https://github.com/TomSchimansky/CustomTkinter.git
synced 2023-08-10 21:13:13 +03:00
Merge branch 'master' into master
This commit is contained in:
@ -29,12 +29,19 @@ class CTkCanvas(tkinter.Canvas):
|
||||
9: 'E', 8: 'F', 7: 'C', 6: 'I', 5: 'E', 4: 'G', 3: 'P', 2: 'R', 1: 'R',
|
||||
0: 'A'}
|
||||
|
||||
radius_to_char_fine_linux = {19: 'A', 18: 'A', 17: 'B', 16: 'B', 15: 'B', 14: 'B', 13: 'F', 12: 'C',
|
||||
11: 'F', 10: 'C',
|
||||
9: 'D', 8: 'G', 7: 'D', 6: 'F', 5: 'D', 4: 'G', 3: 'M', 2: 'H', 1: 'H',
|
||||
0: 'A'}
|
||||
|
||||
if sys.platform.startswith("win"):
|
||||
if sys.getwindowsversion().build > 20000: # Windows 11
|
||||
cls.radius_to_char_fine = radius_to_char_fine_windows_11
|
||||
else: # < Windows 11
|
||||
cls.radius_to_char_fine = radius_to_char_fine_windows_10
|
||||
else: # macOS and Linux
|
||||
elif sys.platform.startswith("linux"): # Optimized on Kali Linux
|
||||
cls.radius_to_char_fine = radius_to_char_fine_linux
|
||||
else:
|
||||
cls.radius_to_char_fine = radius_to_char_fine_windows_10
|
||||
|
||||
def get_char_from_radius(self, radius: int) -> str:
|
||||
|
@ -97,6 +97,19 @@ class CTkCheckBox(CTkBaseClass):
|
||||
self.canvas.bind("<Leave>", self.on_leave)
|
||||
self.canvas.bind("<Button-1>", self.toggle)
|
||||
|
||||
self.text_label = tkinter.Label(master=self,
|
||||
bd=0,
|
||||
text=self.text,
|
||||
justify=tkinter.LEFT,
|
||||
font=self.apply_font_scaling(self.text_font),
|
||||
textvariable=self.textvariable)
|
||||
self.text_label.grid(row=0, column=2, padx=0, pady=0, sticky="w")
|
||||
self.text_label["anchor"] = "w"
|
||||
|
||||
self.text_label.bind("<Enter>", self.on_enter)
|
||||
self.text_label.bind("<Leave>", self.on_leave)
|
||||
self.text_label.bind("<Button-1>", self.toggle)
|
||||
|
||||
# set select state according to variable
|
||||
if self.variable is not None:
|
||||
self.variable_callback_name = self.variable.trace_add("write", self.variable_callback)
|
||||
@ -159,32 +172,18 @@ class CTkCheckBox(CTkBaseClass):
|
||||
outline=ThemeManager.single_color(self.border_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.border_color, self._appearance_mode))
|
||||
|
||||
if self.text_label is None:
|
||||
self.text_label = tkinter.Label(master=self,
|
||||
bd=0,
|
||||
text=self.text,
|
||||
justify=tkinter.LEFT,
|
||||
font=self.apply_font_scaling(self.text_font))
|
||||
self.text_label.grid(row=0, column=2, padx=0, pady=0, sticky="w")
|
||||
self.text_label["anchor"] = "w"
|
||||
|
||||
self.text_label.bind("<Enter>", self.on_enter)
|
||||
self.text_label.bind("<Leave>", self.on_leave)
|
||||
self.text_label.bind("<Button-1>", self.toggle)
|
||||
|
||||
if self.state == tkinter.DISABLED:
|
||||
self.text_label.configure(fg=(ThemeManager.single_color(self.text_color_disabled, self._appearance_mode)))
|
||||
else:
|
||||
self.text_label.configure(fg=ThemeManager.single_color(self.text_color, self._appearance_mode))
|
||||
self.text_label.configure(bg=ThemeManager.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"])
|
||||
self.text = kwargs["text"]
|
||||
self.text_label.configure(text=self.text)
|
||||
del kwargs["text"]
|
||||
|
||||
if "state" in kwargs:
|
||||
@ -225,6 +224,11 @@ class CTkCheckBox(CTkBaseClass):
|
||||
self.function = kwargs["command"]
|
||||
del kwargs["command"]
|
||||
|
||||
if "textvariable" in kwargs:
|
||||
self.textvariable = kwargs["textvariable"]
|
||||
self.text_label.configure(textvariable=self.textvariable)
|
||||
del kwargs["textvariable"]
|
||||
|
||||
if "variable" in kwargs:
|
||||
if self.variable is not None:
|
||||
self.variable.trace_remove("write", self.variable_callback_name)
|
||||
@ -283,13 +287,6 @@ class CTkCheckBox(CTkBaseClass):
|
||||
if self.text_label is not None:
|
||||
self.text_label.configure(cursor="hand2")
|
||||
|
||||
def set_text(self, text):
|
||||
self.text = text
|
||||
if self.text_label is not None:
|
||||
self.text_label.configure(text=self.text)
|
||||
else:
|
||||
sys.stderr.write("ERROR (CTkButton): Cant change text because checkbox has no text.")
|
||||
|
||||
def on_enter(self, event=0):
|
||||
if self.hover is True and self.state == tkinter.NORMAL:
|
||||
if self.check_state is True:
|
||||
@ -370,10 +367,7 @@ class CTkCheckBox(CTkBaseClass):
|
||||
self.variable_callback_blocked = False
|
||||
|
||||
if self.function is not None:
|
||||
try:
|
||||
self.function()
|
||||
except:
|
||||
pass
|
||||
self.function()
|
||||
|
||||
def get(self):
|
||||
return self.onvalue if self.check_state is True else self.offvalue
|
||||
|
@ -12,7 +12,6 @@ from .widget_base_class import CTkBaseClass
|
||||
|
||||
|
||||
class CTkComboBox(CTkBaseClass):
|
||||
|
||||
def __init__(self, *args,
|
||||
bg_color=None,
|
||||
fg_color="default_theme",
|
||||
@ -30,6 +29,7 @@ class CTkComboBox(CTkBaseClass):
|
||||
corner_radius="default_theme",
|
||||
border_width="default_theme",
|
||||
text_font="default_theme",
|
||||
dropdown_text_font="default_theme",
|
||||
text_color="default_theme",
|
||||
text_color_disabled="default_theme",
|
||||
hover=True,
|
||||
@ -44,16 +44,12 @@ class CTkComboBox(CTkBaseClass):
|
||||
self.border_color = ThemeManager.theme["color"]["combobox_border"] if border_color == "default_theme" else border_color
|
||||
self.button_color = ThemeManager.theme["color"]["combobox_border"] if button_color == "default_theme" else button_color
|
||||
self.button_hover_color = ThemeManager.theme["color"]["combobox_button_hover"] if button_hover_color == "default_theme" else button_hover_color
|
||||
self.dropdown_color = ThemeManager.theme["color"]["dropdown_color"] if dropdown_color == "default_theme" else dropdown_color
|
||||
self.dropdown_hover_color = ThemeManager.theme["color"]["dropdown_hover"] if dropdown_hover_color == "default_theme" else dropdown_hover_color
|
||||
self.dropdown_text_color = ThemeManager.theme["color"]["dropdown_text"] if dropdown_text_color == "default_theme" else dropdown_text_color
|
||||
|
||||
# shape
|
||||
self.corner_radius = ThemeManager.theme["shape"]["button_corner_radius"] if corner_radius == "default_theme" else corner_radius
|
||||
self.border_width = ThemeManager.theme["shape"]["entry_border_width"] if border_width == "default_theme" else border_width
|
||||
|
||||
# text and font
|
||||
self.text_label = None
|
||||
self.text_color = ThemeManager.theme["color"]["text"] if text_color == "default_theme" else text_color
|
||||
self.text_color_disabled = ThemeManager.theme["color"]["text_button_disabled"] if text_color_disabled == "default_theme" else text_color_disabled
|
||||
self.text_font = (ThemeManager.theme["text"]["font"], ThemeManager.theme["text"]["size"]) if text_font == "default_theme" else text_font
|
||||
@ -63,7 +59,6 @@ class CTkComboBox(CTkBaseClass):
|
||||
self.variable = variable
|
||||
self.state = state
|
||||
self.hover = hover
|
||||
self.click_animation_running = False
|
||||
|
||||
if values is None:
|
||||
self.values = ["CTkComboBox"]
|
||||
@ -75,7 +70,13 @@ class CTkComboBox(CTkBaseClass):
|
||||
else:
|
||||
self.current_value = "CTkComboBox"
|
||||
|
||||
self.dropdown_menu: Union[DropdownMenu, None] = None
|
||||
self.dropdown_menu = DropdownMenu(master=self,
|
||||
values=self.values,
|
||||
command=self.set,
|
||||
fg_color=dropdown_color,
|
||||
hover_color=dropdown_hover_color,
|
||||
text_color=dropdown_text_color,
|
||||
text_font=dropdown_text_font)
|
||||
|
||||
# configure grid system (1x1)
|
||||
self.grid_rowconfigure(0, weight=1)
|
||||
@ -96,8 +97,11 @@ class CTkComboBox(CTkBaseClass):
|
||||
font=self.apply_font_scaling(self.text_font))
|
||||
left_section_width = self._current_width - self._current_height
|
||||
self.entry.grid(row=0, column=0, rowspan=1, columnspan=1, sticky="ew",
|
||||
padx=(self.apply_widget_scaling(max(self.corner_radius, 3)),
|
||||
self.apply_widget_scaling(max(self._current_width - left_section_width + 3, 3))))
|
||||
padx=(max(self.apply_widget_scaling(self.corner_radius), self.apply_widget_scaling(3)),
|
||||
max(self.apply_widget_scaling(self._current_width - left_section_width + 3), self.apply_widget_scaling(3))))
|
||||
|
||||
self.entry.delete(0, tkinter.END)
|
||||
self.entry.insert(0, self.current_value)
|
||||
|
||||
self.draw() # initial draw
|
||||
|
||||
@ -116,6 +120,13 @@ class CTkComboBox(CTkBaseClass):
|
||||
def set_scaling(self, *args, **kwargs):
|
||||
super().set_scaling(*args, **kwargs)
|
||||
|
||||
# change entry font size and grid padding
|
||||
left_section_width = self._current_width - self._current_height
|
||||
self.entry.configure(font=self.apply_font_scaling(self.text_font))
|
||||
self.entry.grid(row=0, column=0, rowspan=1, columnspan=1, sticky="ew",
|
||||
padx=(max(self.apply_widget_scaling(self.corner_radius), self.apply_widget_scaling(3)),
|
||||
max(self.apply_widget_scaling(self._current_width - left_section_width + 3), self.apply_widget_scaling(3))))
|
||||
|
||||
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
self.draw()
|
||||
@ -139,10 +150,6 @@ class CTkComboBox(CTkBaseClass):
|
||||
self.apply_widget_scaling(self._current_height / 2),
|
||||
self.apply_widget_scaling(self._current_height / 3))
|
||||
|
||||
if self.current_value is not None:
|
||||
self.entry.delete(0, tkinter.END)
|
||||
self.entry.insert(0, self.current_value)
|
||||
|
||||
if no_color_updates is False or requires_recoloring or requires_recoloring_2:
|
||||
|
||||
self.canvas.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
@ -160,28 +167,21 @@ class CTkComboBox(CTkBaseClass):
|
||||
outline=ThemeManager.single_color(self.border_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.border_color, self._appearance_mode))
|
||||
|
||||
self.entry.configure(fg=ThemeManager.single_color(self.text_color, self._appearance_mode))
|
||||
self.entry.configure(bg=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
self.entry.configure(bg=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
fg=ThemeManager.single_color(self.text_color, self._appearance_mode),
|
||||
disabledforeground=ThemeManager.single_color(self.text_color_disabled, self._appearance_mode),
|
||||
disabledbackground=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
|
||||
if self.state == tkinter.DISABLED:
|
||||
self.entry.configure(fg=(ThemeManager.single_color(self.text_color_disabled, self._appearance_mode)))
|
||||
self.canvas.itemconfig("dropdown_arrow",
|
||||
fill=ThemeManager.single_color(self.text_color_disabled, self._appearance_mode))
|
||||
else:
|
||||
self.entry.configure(fg=ThemeManager.single_color(self.text_color, self._appearance_mode))
|
||||
self.canvas.itemconfig("dropdown_arrow",
|
||||
fill=ThemeManager.single_color(self.text_color, self._appearance_mode))
|
||||
|
||||
def open_dropdown_menu(self):
|
||||
self.dropdown_menu = DropdownMenu(x_position=self.winfo_rootx(),
|
||||
y_position=self.winfo_rooty() + self.apply_widget_scaling(self._current_height + 4),
|
||||
width=self._current_width,
|
||||
values=self.values,
|
||||
command=self.set,
|
||||
fg_color=self.dropdown_color,
|
||||
button_hover_color=self.dropdown_hover_color,
|
||||
button_color=self.dropdown_color,
|
||||
text_color=self.dropdown_text_color)
|
||||
self.dropdown_menu.open(self.winfo_rootx(),
|
||||
self.winfo_rooty() + self.apply_widget_scaling(self._current_height + 0))
|
||||
|
||||
def configure(self, *args, **kwargs):
|
||||
require_redraw = False # some attribute changes require a call of self.draw() at the end
|
||||
@ -240,6 +240,23 @@ class CTkComboBox(CTkBaseClass):
|
||||
if "values" in kwargs:
|
||||
self.values = kwargs["values"]
|
||||
del kwargs["values"]
|
||||
self.dropdown_menu.configure(values=self.values)
|
||||
|
||||
if "dropdown_color" in kwargs:
|
||||
self.dropdown_menu.configure(fg_color=kwargs["dropdown_color"])
|
||||
del kwargs["dropdown_color"]
|
||||
|
||||
if "dropdown_hover_color" in kwargs:
|
||||
self.dropdown_menu.configure(hover_color=kwargs["dropdown_hover_color"])
|
||||
del kwargs["dropdown_hover_color"]
|
||||
|
||||
if "dropdown_text_color" in kwargs:
|
||||
self.dropdown_menu.configure(text_color=kwargs["dropdown_text_color"])
|
||||
del kwargs["dropdown_text_color"]
|
||||
|
||||
if "dropdown_text_font" in kwargs:
|
||||
self.dropdown_menu.configure(text_font=kwargs["dropdown_text_font"])
|
||||
del kwargs["dropdown_text_font"]
|
||||
|
||||
super().configure(*args, **kwargs)
|
||||
|
||||
@ -262,8 +279,6 @@ class CTkComboBox(CTkBaseClass):
|
||||
fill=ThemeManager.single_color(self.button_hover_color, self._appearance_mode))
|
||||
|
||||
def on_leave(self, event=0):
|
||||
self.click_animation_running = False
|
||||
|
||||
if self.hover is True:
|
||||
if sys.platform == "darwin" and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
|
||||
self.canvas.configure(cursor="arrow")
|
||||
@ -278,10 +293,6 @@ class CTkComboBox(CTkBaseClass):
|
||||
outline=ThemeManager.single_color(self.button_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.button_color, self._appearance_mode))
|
||||
|
||||
def click_animation(self):
|
||||
if self.click_animation_running:
|
||||
self.on_enter()
|
||||
|
||||
def set(self, value: str, from_variable_callback: bool = False):
|
||||
self.current_value = value
|
||||
|
||||
@ -298,8 +309,3 @@ class CTkComboBox(CTkBaseClass):
|
||||
def clicked(self, event=0):
|
||||
if self.state is not tkinter.DISABLED and len(self.values) > 0:
|
||||
self.open_dropdown_menu()
|
||||
|
||||
# click animation: change color with .on_leave() and back to normal after 100ms with click_animation()
|
||||
self.on_leave()
|
||||
self.click_animation_running = True
|
||||
self.after(100, self.click_animation)
|
||||
|
@ -51,7 +51,7 @@ class CTkEntry(CTkBaseClass):
|
||||
highlightthickness=0,
|
||||
width=self.apply_widget_scaling(self._current_width),
|
||||
height=self.apply_widget_scaling(self._current_height))
|
||||
self.canvas.grid(column=0, row=0, sticky="we")
|
||||
self.canvas.grid(column=0, row=0, sticky="nswe")
|
||||
self.draw_engine = DrawEngine(self.canvas)
|
||||
|
||||
self.entry = tkinter.Entry(master=self,
|
||||
@ -61,15 +61,19 @@ class CTkEntry(CTkBaseClass):
|
||||
font=self.apply_font_scaling(self.text_font),
|
||||
state=self.state,
|
||||
**kwargs)
|
||||
self.entry.grid(column=0, row=0, sticky="we",
|
||||
padx=self.apply_widget_scaling(self.corner_radius) if self.corner_radius >= 6 else self.apply_widget_scaling(6))
|
||||
self.entry.grid(column=0, row=0, sticky="nswe",
|
||||
padx=self.apply_widget_scaling(self.corner_radius) if self.corner_radius >= 6 else self.apply_widget_scaling(6),
|
||||
pady=(self.apply_widget_scaling(self.border_width), self.apply_widget_scaling(self.border_width + 1)))
|
||||
|
||||
super().bind('<Configure>', self.update_dimensions_event)
|
||||
self.entry.bind('<FocusOut>', self.set_placeholder)
|
||||
self.entry.bind('<FocusIn>', self.clear_placeholder)
|
||||
self.entry.bind('<FocusOut>', self.entry_focus_out)
|
||||
self.entry.bind('<FocusIn>', self.entry_focus_in)
|
||||
|
||||
self.draw()
|
||||
self.set_placeholder()
|
||||
|
||||
if self.placeholder_text is not None:
|
||||
self.placeholder_text_active = True
|
||||
self.set_placeholder()
|
||||
|
||||
def set_scaling(self, *args, **kwargs):
|
||||
super().set_scaling( *args, **kwargs)
|
||||
@ -88,23 +92,6 @@ class CTkEntry(CTkBaseClass):
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
self.draw()
|
||||
|
||||
def set_placeholder(self, event=None):
|
||||
if self.placeholder_text is not None:
|
||||
if not self.placeholder_text_active and self.entry.get() == "":
|
||||
self.placeholder_text_active = True
|
||||
self.pre_placeholder_arguments = {"show": self.entry.cget("show")}
|
||||
self.entry.config(fg=ThemeManager.single_color(self.placeholder_text_color, self._appearance_mode), show="")
|
||||
self.entry.delete(0, tkinter.END)
|
||||
self.entry.insert(0, self.placeholder_text)
|
||||
|
||||
def clear_placeholder(self, event=None):
|
||||
if self.placeholder_text_active:
|
||||
self.placeholder_text_active = False
|
||||
self.entry.config(fg=ThemeManager.single_color(self.text_color, self._appearance_mode))
|
||||
self.entry.delete(0, tkinter.END)
|
||||
for argument, value in self.pre_placeholder_arguments.items():
|
||||
self.entry[argument] = value
|
||||
|
||||
def draw(self, no_color_updates=False):
|
||||
self.canvas.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
|
||||
@ -149,35 +136,31 @@ class CTkEntry(CTkBaseClass):
|
||||
require_redraw = False # some attribute changes require a call of self.draw() at the end
|
||||
|
||||
if "state" in kwargs:
|
||||
self.state = kwargs["state"]
|
||||
self.state = kwargs.pop("state")
|
||||
self.entry.configure(state=self.state)
|
||||
del kwargs["state"]
|
||||
|
||||
if "bg_color" in kwargs:
|
||||
if kwargs["bg_color"] is None:
|
||||
new_bg_color = kwargs.pop("bg_color")
|
||||
if new_bg_color is None:
|
||||
self.bg_color = self.detect_color_of_master()
|
||||
else:
|
||||
self.bg_color = kwargs["bg_color"]
|
||||
self.bg_color = new_bg_color
|
||||
require_redraw = True
|
||||
del kwargs["bg_color"]
|
||||
|
||||
if "fg_color" in kwargs:
|
||||
self.fg_color = kwargs["fg_color"]
|
||||
del kwargs["fg_color"]
|
||||
self.fg_color = kwargs.pop("fg_color")
|
||||
require_redraw = True
|
||||
|
||||
if "text_color" in kwargs:
|
||||
self.text_color = kwargs["text_color"]
|
||||
del kwargs["text_color"]
|
||||
self.text_color = kwargs.pop("text_color")
|
||||
require_redraw = True
|
||||
|
||||
if "border_color" in kwargs:
|
||||
self.border_color = kwargs["border_color"]
|
||||
del kwargs["border_color"]
|
||||
self.border_color = kwargs.pop("border_color")
|
||||
require_redraw = True
|
||||
|
||||
if "corner_radius" in kwargs:
|
||||
self.corner_radius = kwargs["corner_radius"]
|
||||
self.corner_radius = kwargs.pop("corner_radius")
|
||||
|
||||
if self.corner_radius * 2 > self._current_height:
|
||||
self.corner_radius = self._current_height / 2
|
||||
@ -185,31 +168,69 @@ class CTkEntry(CTkBaseClass):
|
||||
self.corner_radius = self._current_width / 2
|
||||
|
||||
self.entry.grid(column=0, row=0, sticky="we", padx=self.apply_widget_scaling(self.corner_radius) if self.corner_radius >= 6 else self.apply_widget_scaling(6))
|
||||
del kwargs["corner_radius"]
|
||||
require_redraw = True
|
||||
|
||||
if "width" in kwargs:
|
||||
self.set_dimensions(width=kwargs["width"])
|
||||
del kwargs["width"]
|
||||
self.set_dimensions(width=kwargs.pop("width"))
|
||||
|
||||
if "height" in kwargs:
|
||||
self.set_dimensions(height=kwargs["height"])
|
||||
del kwargs["height"]
|
||||
self.set_dimensions(height=kwargs.pop("height"))
|
||||
|
||||
if "placeholder_text" in kwargs:
|
||||
pass
|
||||
self.placeholder_text = kwargs.pop("placeholder_text")
|
||||
if self.placeholder_text_active:
|
||||
self.entry.delete(0, tkinter.END)
|
||||
self.entry.insert(0, self.placeholder_text)
|
||||
|
||||
if "placeholder_text_color" in kwargs:
|
||||
self.placeholder_text_color = kwargs.pop("placeholder_text_color")
|
||||
require_redraw = True
|
||||
|
||||
if "show" in kwargs:
|
||||
if self.placeholder_text_active:
|
||||
self.pre_placeholder_arguments["show"] = kwargs.pop("show")
|
||||
else:
|
||||
self.entry.configure(show=kwargs.pop("show"))
|
||||
|
||||
self.entry.configure(*args, **kwargs)
|
||||
|
||||
if require_redraw is True:
|
||||
self.draw()
|
||||
|
||||
def set_placeholder(self):
|
||||
self.pre_placeholder_arguments = {"show": self.entry.cget("show")}
|
||||
self.entry.config(fg=ThemeManager.single_color(self.placeholder_text_color, self._appearance_mode), show="")
|
||||
self.entry.delete(0, tkinter.END)
|
||||
self.entry.insert(0, self.placeholder_text)
|
||||
|
||||
def clear_placeholder(self):
|
||||
self.entry.config(fg=ThemeManager.single_color(self.text_color, self._appearance_mode))
|
||||
self.entry.delete(0, tkinter.END)
|
||||
for argument, value in self.pre_placeholder_arguments.items():
|
||||
self.entry[argument] = value
|
||||
|
||||
def entry_focus_out(self, event=None):
|
||||
if self.entry.get() == "":
|
||||
self.placeholder_text_active = True
|
||||
self.set_placeholder()
|
||||
|
||||
def entry_focus_in(self, event=None):
|
||||
if self.placeholder_text_active:
|
||||
self.placeholder_text_active = False
|
||||
self.clear_placeholder()
|
||||
|
||||
def delete(self, *args, **kwargs):
|
||||
self.entry.delete(*args, **kwargs)
|
||||
self.set_placeholder()
|
||||
|
||||
if self.entry.get() == "":
|
||||
self.placeholder_text_active = True
|
||||
self.set_placeholder()
|
||||
|
||||
def insert(self, *args, **kwargs):
|
||||
self.clear_placeholder()
|
||||
if self.placeholder_text_active:
|
||||
self.placeholder_text_active = False
|
||||
self.clear_placeholder()
|
||||
|
||||
return self.entry.insert(*args, **kwargs)
|
||||
|
||||
def get(self):
|
||||
@ -217,3 +238,9 @@ class CTkEntry(CTkBaseClass):
|
||||
return ""
|
||||
else:
|
||||
return self.entry.get()
|
||||
|
||||
def focus(self):
|
||||
self.entry.focus()
|
||||
|
||||
def focus_force(self):
|
||||
self.entry.focus_force()
|
||||
|
@ -16,6 +16,7 @@ class CTkLabel(CTkBaseClass):
|
||||
height=28,
|
||||
text="CTkLabel",
|
||||
text_font="default_theme",
|
||||
anchor="center", # label anchor: center, n, e, s, w
|
||||
**kwargs):
|
||||
|
||||
# transfer basic functionality (bg_color, size, _appearance_mode, scaling) to CTkBaseClass
|
||||
@ -35,6 +36,7 @@ class CTkLabel(CTkBaseClass):
|
||||
self.corner_radius = ThemeManager.theme["shape"]["label_corner_radius"] if corner_radius == "default_theme" else corner_radius
|
||||
|
||||
# text
|
||||
self.anchor = anchor
|
||||
self.text = text
|
||||
self.text_font = (ThemeManager.theme["text"]["font"], ThemeManager.theme["text"]["size"]) if text_font == "default_theme" else text_font
|
||||
|
||||
@ -52,10 +54,13 @@ class CTkLabel(CTkBaseClass):
|
||||
self.text_label = tkinter.Label(master=self,
|
||||
highlightthickness=0,
|
||||
bd=0,
|
||||
anchor=self.anchor,
|
||||
text=self.text,
|
||||
font=self.apply_font_scaling(self.text_font),
|
||||
**kwargs)
|
||||
self.text_label.grid(row=0, column=0, padx=self.apply_widget_scaling(self.corner_radius))
|
||||
text_label_grid_sticky = self.anchor if self.anchor != "center" else ""
|
||||
self.text_label.grid(row=0, column=0, padx=self.apply_widget_scaling(self.corner_radius),
|
||||
sticky=text_label_grid_sticky)
|
||||
|
||||
self.bind('<Configure>', self.update_dimensions_event)
|
||||
self.draw()
|
||||
@ -65,7 +70,9 @@ class CTkLabel(CTkBaseClass):
|
||||
|
||||
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width), height=self.apply_widget_scaling(self._desired_height))
|
||||
self.text_label.configure(font=self.apply_font_scaling(self.text_font))
|
||||
self.text_label.grid(row=0, column=0, padx=self.apply_widget_scaling(self.corner_radius))
|
||||
text_label_grid_sticky = self.anchor if self.anchor != "center" else ""
|
||||
self.text_label.grid(row=0, column=0, padx=self.apply_widget_scaling(self.corner_radius),
|
||||
sticky=text_label_grid_sticky)
|
||||
|
||||
self.draw()
|
||||
|
||||
@ -103,6 +110,12 @@ class CTkLabel(CTkBaseClass):
|
||||
def configure(self, *args, **kwargs):
|
||||
require_redraw = False # some attribute changes require a call of self.draw() at the end
|
||||
|
||||
if "anchor" in kwargs:
|
||||
self.anchor = kwargs.pop("anchor")
|
||||
text_label_grid_sticky = self.anchor if self.anchor != "center" else ""
|
||||
self.text_label.grid(row=0, column=0, padx=self.apply_widget_scaling(self.corner_radius),
|
||||
sticky=text_label_grid_sticky)
|
||||
|
||||
if "text" in kwargs:
|
||||
self.set_text(kwargs["text"])
|
||||
del kwargs["text"]
|
||||
|
@ -1,6 +1,5 @@
|
||||
import tkinter
|
||||
import sys
|
||||
from typing import Union
|
||||
|
||||
from .dropdown_menu import DropdownMenu
|
||||
|
||||
@ -12,12 +11,13 @@ from .widget_base_class import CTkBaseClass
|
||||
|
||||
|
||||
class CTkOptionMenu(CTkBaseClass):
|
||||
|
||||
def __init__(self, *args,
|
||||
bg_color=None,
|
||||
fg_color="default_theme",
|
||||
button_color="default_theme",
|
||||
button_hover_color="default_theme",
|
||||
text_color="default_theme",
|
||||
text_color_disabled="default_theme",
|
||||
dropdown_color="default_theme",
|
||||
dropdown_hover_color="default_theme",
|
||||
dropdown_text_color="default_theme",
|
||||
@ -28,10 +28,10 @@ class CTkOptionMenu(CTkBaseClass):
|
||||
height=28,
|
||||
corner_radius="default_theme",
|
||||
text_font="default_theme",
|
||||
text_color="default_theme",
|
||||
text_color_disabled="default_theme",
|
||||
dropdown_text_font="default_theme",
|
||||
hover=True,
|
||||
state=tkinter.NORMAL,
|
||||
dynamic_resizing=True,
|
||||
**kwargs):
|
||||
|
||||
# transfer basic functionality (bg_color, size, _appearance_mode, scaling) to CTkBaseClass
|
||||
@ -41,18 +41,15 @@ class CTkOptionMenu(CTkBaseClass):
|
||||
self.fg_color = ThemeManager.theme["color"]["button"] if fg_color == "default_theme" else fg_color
|
||||
self.button_color = ThemeManager.theme["color"]["optionmenu_button"] if button_color == "default_theme" else button_color
|
||||
self.button_hover_color = ThemeManager.theme["color"]["optionmenu_button_hover"] if button_hover_color == "default_theme" else button_hover_color
|
||||
self.dropdown_color = ThemeManager.theme["color"]["dropdown_color"] if dropdown_color == "default_theme" else dropdown_color
|
||||
self.dropdown_hover_color = ThemeManager.theme["color"]["dropdown_hover"] if dropdown_hover_color == "default_theme" else dropdown_hover_color
|
||||
self.dropdown_text_color = ThemeManager.theme["color"]["dropdown_text"] if dropdown_text_color == "default_theme" else dropdown_text_color
|
||||
|
||||
# shape
|
||||
self.corner_radius = ThemeManager.theme["shape"]["button_corner_radius"] if corner_radius == "default_theme" else corner_radius
|
||||
|
||||
# text and font
|
||||
self.text_label = None
|
||||
self.text_color = ThemeManager.theme["color"]["text"] if text_color == "default_theme" else text_color
|
||||
self.text_color_disabled = ThemeManager.theme["color"]["text_button_disabled"] if text_color_disabled == "default_theme" else text_color_disabled
|
||||
self.text_font = (ThemeManager.theme["text"]["font"], ThemeManager.theme["text"]["size"]) if text_font == "default_theme" else text_font
|
||||
self.dropdown_text_font = dropdown_text_font
|
||||
|
||||
# callback and hover functionality
|
||||
self.function = command
|
||||
@ -61,7 +58,7 @@ class CTkOptionMenu(CTkBaseClass):
|
||||
self.variable_callback_name = None
|
||||
self.state = state
|
||||
self.hover = hover
|
||||
self.click_animation_running = False
|
||||
self.dynamic_resizing = dynamic_resizing
|
||||
|
||||
if values is None:
|
||||
self.values = ["CTkOptionMenu"]
|
||||
@ -73,7 +70,13 @@ class CTkOptionMenu(CTkBaseClass):
|
||||
else:
|
||||
self.current_value = "CTkOptionMenu"
|
||||
|
||||
self.dropdown_menu: Union[DropdownMenu, None] = None
|
||||
self.dropdown_menu = DropdownMenu(master=self,
|
||||
values=self.values,
|
||||
command=self.set,
|
||||
fg_color=dropdown_color,
|
||||
hover_color=dropdown_hover_color,
|
||||
text_color=dropdown_text_color,
|
||||
text_font=dropdown_text_font)
|
||||
|
||||
# configure grid system (1x1)
|
||||
self.grid_rowconfigure(0, weight=1)
|
||||
@ -86,11 +89,35 @@ class CTkOptionMenu(CTkBaseClass):
|
||||
self.canvas.grid(row=0, column=0, rowspan=1, columnspan=1, sticky="nsew")
|
||||
self.draw_engine = DrawEngine(self.canvas)
|
||||
|
||||
left_section_width = self._current_width - self._current_height
|
||||
self.text_label = tkinter.Label(master=self,
|
||||
font=self.apply_font_scaling(self.text_font),
|
||||
anchor="w",
|
||||
text=self.current_value)
|
||||
self.text_label.grid(row=0, column=0, sticky="w",
|
||||
padx=(max(self.apply_widget_scaling(self.corner_radius), self.apply_widget_scaling(3)),
|
||||
max(self.apply_widget_scaling(self._current_width - left_section_width + 3), self.apply_widget_scaling(3))))
|
||||
|
||||
if not self.dynamic_resizing:
|
||||
self.grid_propagate(0)
|
||||
|
||||
if Settings.cursor_manipulation_enabled:
|
||||
if sys.platform == "darwin":
|
||||
self.configure(cursor="pointinghand")
|
||||
elif sys.platform.startswith("win"):
|
||||
self.configure(cursor="hand2")
|
||||
|
||||
# event bindings
|
||||
self.canvas.bind("<Enter>", self.on_enter)
|
||||
self.canvas.bind("<Leave>", self.on_leave)
|
||||
self.canvas.bind("<Button-1>", self.clicked)
|
||||
self.canvas.bind("<Button-1>", self.clicked)
|
||||
|
||||
self.text_label.bind("<Enter>", self.on_enter)
|
||||
self.text_label.bind("<Leave>", self.on_leave)
|
||||
self.text_label.bind("<Button-1>", self.clicked)
|
||||
self.text_label.bind("<Button-1>", self.clicked)
|
||||
|
||||
self.bind('<Configure>', self.update_dimensions_event)
|
||||
|
||||
self.draw() # initial draw
|
||||
@ -102,9 +129,12 @@ class CTkOptionMenu(CTkBaseClass):
|
||||
def set_scaling(self, *args, **kwargs):
|
||||
super().set_scaling(*args, **kwargs)
|
||||
|
||||
if self.text_label is not None:
|
||||
self.text_label.destroy()
|
||||
self.text_label = None
|
||||
# change label text size and grid padding
|
||||
left_section_width = self._current_width - self._current_height
|
||||
self.text_label.configure(font=self.apply_font_scaling(self.text_font))
|
||||
self.text_label.grid(row=0, column=0, sticky="w",
|
||||
padx=(max(self.apply_widget_scaling(self.corner_radius), self.apply_widget_scaling(3)),
|
||||
max(self.apply_widget_scaling(self._current_width - left_section_width + 3), self.apply_widget_scaling(3))))
|
||||
|
||||
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
@ -128,20 +158,6 @@ class CTkOptionMenu(CTkBaseClass):
|
||||
requires_recoloring_2 = self.draw_engine.draw_dropdown_arrow(self.apply_widget_scaling(self._current_width - (self._current_height / 2)),
|
||||
self.apply_widget_scaling(self._current_height / 2),
|
||||
self.apply_widget_scaling(self._current_height / 3))
|
||||
if self.text_label is None:
|
||||
self.text_label = tkinter.Label(master=self,
|
||||
font=self.apply_font_scaling(self.text_font))
|
||||
self.text_label.grid(row=0, column=0, rowspan=1, columnspan=1, sticky="w",
|
||||
padx=(max(self.apply_widget_scaling(self.corner_radius), 3),
|
||||
max(self._current_width - left_section_width + 3, 3)))
|
||||
|
||||
self.text_label.bind("<Enter>", self.on_enter)
|
||||
self.text_label.bind("<Leave>", self.on_leave)
|
||||
self.text_label.bind("<Button-1>", self.clicked)
|
||||
self.text_label.bind("<Button-1>", self.clicked)
|
||||
|
||||
if self.current_value is not None:
|
||||
self.text_label.configure(text=self.current_value)
|
||||
|
||||
if no_color_updates is False or requires_recoloring or requires_recoloring_2:
|
||||
|
||||
@ -167,62 +183,51 @@ class CTkOptionMenu(CTkBaseClass):
|
||||
|
||||
self.text_label.configure(bg=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
|
||||
self.canvas.update_idletasks()
|
||||
|
||||
def open_dropdown_menu(self):
|
||||
self.dropdown_menu = DropdownMenu(x_position=self.winfo_rootx(),
|
||||
y_position=self.winfo_rooty() + self.apply_widget_scaling(self._current_height + 4),
|
||||
width=self._current_width,
|
||||
values=self.values,
|
||||
command=self.set,
|
||||
fg_color=self.dropdown_color,
|
||||
button_hover_color=self.dropdown_hover_color,
|
||||
button_color=self.dropdown_color,
|
||||
text_color=self.dropdown_text_color)
|
||||
self.dropdown_menu.open(self.winfo_rootx(),
|
||||
self.winfo_rooty() + self.apply_widget_scaling(self._current_height + 0))
|
||||
|
||||
def configure(self, *args, **kwargs):
|
||||
require_redraw = False # some attribute changes require a call of self.draw() at the end
|
||||
|
||||
if "state" in kwargs:
|
||||
self.state = kwargs["state"]
|
||||
self.state = kwargs.pop("state")
|
||||
require_redraw = True
|
||||
del kwargs["state"]
|
||||
|
||||
if "fg_color" in kwargs:
|
||||
self.fg_color = kwargs["fg_color"]
|
||||
self.fg_color = kwargs.pop("fg_color")
|
||||
require_redraw = True
|
||||
del kwargs["fg_color"]
|
||||
|
||||
if "bg_color" in kwargs:
|
||||
if kwargs["bg_color"] is None:
|
||||
new_bg_color = kwargs.pop("bg_color")
|
||||
if new_bg_color is None:
|
||||
self.bg_color = self.detect_color_of_master()
|
||||
else:
|
||||
self.bg_color = kwargs["bg_color"]
|
||||
self.bg_color = new_bg_color
|
||||
require_redraw = True
|
||||
del kwargs["bg_color"]
|
||||
|
||||
if "button_color" in kwargs:
|
||||
self.button_color = kwargs["button_color"]
|
||||
self.button_color = kwargs.pop("button_color")
|
||||
require_redraw = True
|
||||
del kwargs["button_color"]
|
||||
|
||||
if "button_hover_color" in kwargs:
|
||||
self.button_hover_color = kwargs["button_hover_color"]
|
||||
self.button_hover_color = kwargs.pop("button_hover_color")
|
||||
require_redraw = True
|
||||
del kwargs["button_hover_color"]
|
||||
|
||||
if "text_color" in kwargs:
|
||||
self.text_color = kwargs["text_color"]
|
||||
self.text_color = kwargs.pop("text_color")
|
||||
require_redraw = True
|
||||
del kwargs["text_color"]
|
||||
|
||||
if "command" in kwargs:
|
||||
self.function = kwargs["command"]
|
||||
del kwargs["command"]
|
||||
self.function = kwargs.pop("command")
|
||||
|
||||
if "variable" in kwargs:
|
||||
if self.variable is not None: # remove old callback
|
||||
self.variable.trace_remove("write", self.variable_callback_name)
|
||||
|
||||
self.variable = kwargs["variable"]
|
||||
self.variable = kwargs.pop("variable")
|
||||
|
||||
if self.variable is not None and self.variable != "":
|
||||
self.variable_callback_name = self.variable.trace_add("write", self.variable_callback)
|
||||
@ -230,19 +235,34 @@ class CTkOptionMenu(CTkBaseClass):
|
||||
else:
|
||||
self.variable = None
|
||||
|
||||
del kwargs["variable"]
|
||||
|
||||
if "width" in kwargs:
|
||||
self.set_dimensions(width=kwargs["width"])
|
||||
del kwargs["width"]
|
||||
self.set_dimensions(width=kwargs.pop("width"))
|
||||
|
||||
if "height" in kwargs:
|
||||
self.set_dimensions(height=kwargs["height"])
|
||||
del kwargs["height"]
|
||||
self.set_dimensions(height=kwargs.pop("height"))
|
||||
|
||||
if "values" in kwargs:
|
||||
self.values = kwargs["values"]
|
||||
del kwargs["values"]
|
||||
self.values = kwargs.pop("values")
|
||||
self.dropdown_menu.configure(values=self.values)
|
||||
|
||||
if "dropdown_color" in kwargs:
|
||||
self.dropdown_menu.configure(fg_color=kwargs.pop("dropdown_color"))
|
||||
|
||||
if "dropdown_hover_color" in kwargs:
|
||||
self.dropdown_menu.configure(hover_color=kwargs.pop("dropdown_hover_color"))
|
||||
|
||||
if "dropdown_text_color" in kwargs:
|
||||
self.dropdown_menu.configure(text_color=kwargs.pop("dropdown_text_color"))
|
||||
|
||||
if "dropdown_text_font" in kwargs:
|
||||
self.dropdown_menu.configure(text_font=kwargs.pop("dropdown_text_font"))
|
||||
|
||||
if "dynamic_resizing" in kwargs:
|
||||
self.dynamic_resizing = kwargs.pop("dynamic_resizing")
|
||||
if not self.dynamic_resizing:
|
||||
self.grid_propagate(0)
|
||||
else:
|
||||
self.grid_propagate(1)
|
||||
|
||||
super().configure(*args, **kwargs)
|
||||
|
||||
@ -251,34 +271,18 @@ class CTkOptionMenu(CTkBaseClass):
|
||||
|
||||
def on_enter(self, event=0):
|
||||
if self.hover is True and self.state == tkinter.NORMAL and len(self.values) > 0:
|
||||
if sys.platform == "darwin" and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
|
||||
self.configure(cursor="pointinghand")
|
||||
elif sys.platform.startswith("win") and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
|
||||
self.configure(cursor="hand2")
|
||||
|
||||
# set color of inner button parts to hover color
|
||||
self.canvas.itemconfig("inner_parts_right",
|
||||
outline=ThemeManager.single_color(self.button_hover_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.button_hover_color, self._appearance_mode))
|
||||
|
||||
def on_leave(self, event=0):
|
||||
self.click_animation_running = False
|
||||
|
||||
if self.hover is True:
|
||||
if sys.platform == "darwin" and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
|
||||
self.configure(cursor="arrow")
|
||||
elif sys.platform.startswith("win") and len(self.values) > 0 and Settings.cursor_manipulation_enabled:
|
||||
self.configure(cursor="arrow")
|
||||
|
||||
# set color of inner button parts
|
||||
self.canvas.itemconfig("inner_parts_right",
|
||||
outline=ThemeManager.single_color(self.button_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.button_color, self._appearance_mode))
|
||||
|
||||
def click_animation(self):
|
||||
if self.click_animation_running:
|
||||
self.on_enter()
|
||||
|
||||
def variable_callback(self, var_name, index, mode):
|
||||
if not self.variable_callback_blocked:
|
||||
self.set(self.variable.get(), from_variable_callback=True)
|
||||
@ -286,10 +290,7 @@ class CTkOptionMenu(CTkBaseClass):
|
||||
def set(self, value: str, from_variable_callback: bool = False):
|
||||
self.current_value = value
|
||||
|
||||
if self.text_label is not None:
|
||||
self.text_label.configure(text=self.current_value)
|
||||
else:
|
||||
self.draw()
|
||||
self.text_label.configure(text=self.current_value)
|
||||
|
||||
if self.variable is not None and not from_variable_callback:
|
||||
self.variable_callback_blocked = True
|
||||
@ -306,8 +307,3 @@ class CTkOptionMenu(CTkBaseClass):
|
||||
def clicked(self, event=0):
|
||||
if self.state is not tkinter.DISABLED and len(self.values) > 0:
|
||||
self.open_dropdown_menu()
|
||||
|
||||
# click animation: change color with .on_leave() and back to normal after 100ms with click_animation()
|
||||
self.on_leave()
|
||||
self.click_animation_running = True
|
||||
self.after(100, self.click_animation)
|
||||
|
@ -1,6 +1,6 @@
|
||||
import tkinter
|
||||
import sys
|
||||
from typing import Callable, Union
|
||||
from typing import Union
|
||||
|
||||
from .ctk_canvas import CTkCanvas
|
||||
from ..theme_manager import ThemeManager
|
||||
@ -86,6 +86,19 @@ class CTkRadioButton(CTkBaseClass):
|
||||
self.canvas.bind("<Leave>", self.on_leave)
|
||||
self.canvas.bind("<Button-1>", self.invoke)
|
||||
|
||||
self.text_label = tkinter.Label(master=self,
|
||||
bd=0,
|
||||
text=self.text,
|
||||
justify=tkinter.LEFT,
|
||||
font=self.apply_font_scaling(self.text_font),
|
||||
textvariable=self.textvariable)
|
||||
self.text_label.grid(row=0, column=2, padx=0, pady=0, sticky="w")
|
||||
self.text_label["anchor"] = "w"
|
||||
|
||||
self.text_label.bind("<Enter>", self.on_enter)
|
||||
self.text_label.bind("<Leave>", self.on_leave)
|
||||
self.text_label.bind("<Button-1>", self.invoke)
|
||||
|
||||
self.draw() # initial draw
|
||||
self.set_cursor()
|
||||
|
||||
@ -134,19 +147,6 @@ class CTkRadioButton(CTkBaseClass):
|
||||
outline=ThemeManager.single_color(self.bg_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
|
||||
if self.text_label is None:
|
||||
self.text_label = tkinter.Label(master=self,
|
||||
bd=0,
|
||||
text=self.text,
|
||||
justify=tkinter.LEFT,
|
||||
font=self.apply_font_scaling(self.text_font))
|
||||
self.text_label.grid(row=0, column=2, padx=0, pady=0, sticky="w")
|
||||
self.text_label["anchor"] = "w"
|
||||
|
||||
self.text_label.bind("<Enter>", self.on_enter)
|
||||
self.text_label.bind("<Leave>", self.on_leave)
|
||||
self.text_label.bind("<Button-1>", self.invoke)
|
||||
|
||||
if self.state == tkinter.DISABLED:
|
||||
self.text_label.configure(fg=ThemeManager.single_color(self.text_color_disabled, self._appearance_mode))
|
||||
else:
|
||||
@ -154,63 +154,58 @@ class CTkRadioButton(CTkBaseClass):
|
||||
|
||||
self.text_label.configure(bg=ThemeManager.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"]
|
||||
self.text = kwargs.pop("text")
|
||||
self.text_label.configure(text=self.text)
|
||||
|
||||
if "state" in kwargs:
|
||||
self.state = kwargs["state"]
|
||||
self.state = kwargs.pop("state")
|
||||
self.set_cursor()
|
||||
require_redraw = True
|
||||
del kwargs["state"]
|
||||
|
||||
if "fg_color" in kwargs:
|
||||
self.fg_color = kwargs["fg_color"]
|
||||
self.fg_color = kwargs.pop("fg_color")
|
||||
require_redraw = True
|
||||
del kwargs["fg_color"]
|
||||
|
||||
if "bg_color" in kwargs:
|
||||
if kwargs["bg_color"] is None:
|
||||
new_bg_color = kwargs.pop("bg_color")
|
||||
if new_bg_color is None:
|
||||
self.bg_color = self.detect_color_of_master()
|
||||
else:
|
||||
self.bg_color = kwargs["bg_color"]
|
||||
self.bg_color = new_bg_color
|
||||
require_redraw = True
|
||||
del kwargs["bg_color"]
|
||||
|
||||
if "hover_color" in kwargs:
|
||||
self.hover_color = kwargs["hover_color"]
|
||||
self.hover_color = kwargs.pop("hover_color")
|
||||
require_redraw = True
|
||||
del kwargs["hover_color"]
|
||||
|
||||
if "text_color" in kwargs:
|
||||
self.text_color = kwargs["text_color"]
|
||||
self.text_color = kwargs.pop("text_color")
|
||||
require_redraw = True
|
||||
del kwargs["text_color"]
|
||||
|
||||
if "border_color" in kwargs:
|
||||
self.border_color = kwargs["border_color"]
|
||||
self.border_color = kwargs.pop("border_color")
|
||||
require_redraw = True
|
||||
del kwargs["border_color"]
|
||||
|
||||
if "border_width" in kwargs:
|
||||
self.border_width = kwargs["border_width"]
|
||||
self.border_width = kwargs.pop("border_width")
|
||||
require_redraw = True
|
||||
del kwargs["border_width"]
|
||||
|
||||
if "command" in kwargs:
|
||||
self.function = kwargs["command"]
|
||||
del kwargs["command"]
|
||||
self.function = kwargs.pop("command")
|
||||
|
||||
if "textvariable" in kwargs:
|
||||
self.textvariable = kwargs.pop("textvariable")
|
||||
self.text_label.configure(textvariable=self.textvariable)
|
||||
|
||||
if "variable" in kwargs:
|
||||
if self.variable is not None:
|
||||
self.variable.trace_remove("write", self.variable_callback_name)
|
||||
|
||||
self.variable = kwargs["variable"]
|
||||
self.variable = kwargs.pop("variable")
|
||||
|
||||
if self.variable is not None and self.variable != "":
|
||||
self.variable_callback_name = self.variable.trace_add("write", self.variable_callback)
|
||||
@ -221,8 +216,6 @@ class CTkRadioButton(CTkBaseClass):
|
||||
else:
|
||||
self.variable = None
|
||||
|
||||
del kwargs["variable"]
|
||||
|
||||
super().configure(*args, **kwargs)
|
||||
|
||||
if require_redraw:
|
||||
@ -250,13 +243,6 @@ class CTkRadioButton(CTkBaseClass):
|
||||
if self.text_label is not None:
|
||||
self.text_label.configure(cursor="hand2")
|
||||
|
||||
def set_text(self, text):
|
||||
self.text = text
|
||||
if self.text_label is not None:
|
||||
self.text_label.configure(text=self.text)
|
||||
else:
|
||||
sys.stderr.write("ERROR (CTkButton): Cant change text because radiobutton has no text.")
|
||||
|
||||
def on_enter(self, event=0):
|
||||
if self.hover is True and self.state == tkinter.NORMAL:
|
||||
self.canvas.itemconfig("border_parts",
|
||||
@ -287,11 +273,8 @@ class CTkRadioButton(CTkBaseClass):
|
||||
self.check_state = True
|
||||
self.select()
|
||||
|
||||
if self.function is not None:
|
||||
try:
|
||||
self.function()
|
||||
except:
|
||||
pass
|
||||
if self.function is not None:
|
||||
self.function()
|
||||
|
||||
def select(self, from_variable_callback=False):
|
||||
self.check_state = True
|
||||
|
238
customtkinter/widgets/ctk_scrollbar.py
Normal file
238
customtkinter/widgets/ctk_scrollbar.py
Normal file
@ -0,0 +1,238 @@
|
||||
import sys
|
||||
|
||||
from .ctk_canvas import CTkCanvas
|
||||
from ..theme_manager import ThemeManager
|
||||
from ..draw_engine import DrawEngine
|
||||
from .widget_base_class import CTkBaseClass
|
||||
|
||||
|
||||
class CTkScrollbar(CTkBaseClass):
|
||||
def __init__(self, *args,
|
||||
bg_color=None,
|
||||
fg_color="default_theme",
|
||||
scrollbar_color="default_theme",
|
||||
scrollbar_hover_color="default_theme",
|
||||
border_spacing="default_theme",
|
||||
corner_radius="default_theme",
|
||||
width=None,
|
||||
height=None,
|
||||
minimum_pixel_length=20,
|
||||
orientation="vertical",
|
||||
command=None,
|
||||
hover=True,
|
||||
**kwargs):
|
||||
|
||||
# set default dimensions according to orientation
|
||||
if width is None:
|
||||
if orientation.lower() == "vertical":
|
||||
width = 16
|
||||
else:
|
||||
width = 200
|
||||
if height is None:
|
||||
if orientation.lower() == "horizontal":
|
||||
height = 16
|
||||
else:
|
||||
height = 200
|
||||
|
||||
# transfer basic functionality (bg_color, size, _appearance_mode, scaling) to CTkBaseClass
|
||||
super().__init__(*args, bg_color=bg_color, width=width, height=height, **kwargs)
|
||||
|
||||
# color
|
||||
self.fg_color = ThemeManager.theme["color"]["frame_high"] if fg_color == "default_theme" else fg_color
|
||||
self.scrollbar_color = ThemeManager.theme["color"]["scrollbar_button"] if scrollbar_color == "default_theme" else scrollbar_color
|
||||
self.scrollbar_hover_color = ThemeManager.theme["color"]["scrollbar_button_hover"] if scrollbar_hover_color == "default_theme" else scrollbar_hover_color
|
||||
|
||||
# shape
|
||||
self.corner_radius = ThemeManager.theme["shape"]["scrollbar_corner_radius"] if corner_radius == "default_theme" else corner_radius
|
||||
self.border_spacing = ThemeManager.theme["shape"]["scrollbar_border_spacing"] if border_spacing == "default_theme" else border_spacing
|
||||
|
||||
self.hover = hover
|
||||
self.hover_state = False
|
||||
self.command = command
|
||||
self.orientation = orientation
|
||||
self.start_value: float = 0 # 0 to 1
|
||||
self.end_value: float = 1 # 0 to 1
|
||||
self.minimum_pixel_length = minimum_pixel_length
|
||||
|
||||
self.canvas = CTkCanvas(master=self,
|
||||
highlightthickness=0,
|
||||
width=self.apply_widget_scaling(self._current_width),
|
||||
height=self.apply_widget_scaling(self._current_height))
|
||||
self.canvas.place(x=0, y=0, relwidth=1, relheight=1)
|
||||
self.draw_engine = DrawEngine(self.canvas)
|
||||
|
||||
self.canvas.bind("<Enter>", self.on_enter)
|
||||
self.canvas.bind("<Leave>", self.on_leave)
|
||||
self.canvas.tag_bind("border_parts", "<Button-1>", self.clicked)
|
||||
self.canvas.bind("<B1-Motion>", self.clicked)
|
||||
self.canvas.bind("<MouseWheel>", self.mouse_scroll_event)
|
||||
self.bind('<Configure>', self.update_dimensions_event)
|
||||
|
||||
self.draw()
|
||||
|
||||
def set_scaling(self, *args, **kwargs):
|
||||
super().set_scaling(*args, **kwargs)
|
||||
|
||||
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width), height=self.apply_widget_scaling(self._desired_height))
|
||||
self.draw(no_color_updates=True)
|
||||
|
||||
def set_dimensions(self, width=None, height=None):
|
||||
super().set_dimensions(width, height)
|
||||
|
||||
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
self.draw(no_color_updates=True)
|
||||
|
||||
def get_scrollbar_values_for_minimum_pixel_size(self):
|
||||
# correct scrollbar float values if scrollbar is too small
|
||||
if self.orientation == "vertical":
|
||||
scrollbar_pixel_length = (self.end_value - self.start_value) * self._current_height
|
||||
if scrollbar_pixel_length < self.minimum_pixel_length and -scrollbar_pixel_length + self._current_height != 0:
|
||||
# calculate how much to increase the float interval values so that the scrollbar width is self.minimum_pixel_length
|
||||
interval_extend_factor = (-scrollbar_pixel_length + self.minimum_pixel_length) / (-scrollbar_pixel_length + self._current_height)
|
||||
corrected_end_value = self.end_value + (1 - self.end_value) * interval_extend_factor
|
||||
corrected_start_value = self.start_value - self.start_value * interval_extend_factor
|
||||
return corrected_start_value, corrected_end_value
|
||||
else:
|
||||
return self.start_value, self.end_value
|
||||
|
||||
else:
|
||||
scrollbar_pixel_length = (self.end_value - self.start_value) * self._current_width
|
||||
if scrollbar_pixel_length < self.minimum_pixel_length and -scrollbar_pixel_length + self._current_width != 0:
|
||||
# calculate how much to increase the float interval values so that the scrollbar width is self.minimum_pixel_length
|
||||
interval_extend_factor = (-scrollbar_pixel_length + self.minimum_pixel_length) / (-scrollbar_pixel_length + self._current_width)
|
||||
corrected_end_value = self.end_value + (1 - self.end_value) * interval_extend_factor
|
||||
corrected_start_value = self.start_value - self.start_value * interval_extend_factor
|
||||
return corrected_start_value, corrected_end_value
|
||||
else:
|
||||
return self.start_value, self.end_value
|
||||
|
||||
def draw(self, no_color_updates=False):
|
||||
corrected_start_value, corrected_end_value = self.get_scrollbar_values_for_minimum_pixel_size()
|
||||
requires_recoloring = self.draw_engine.draw_rounded_scrollbar(self.apply_widget_scaling(self._current_width),
|
||||
self.apply_widget_scaling(self._current_height),
|
||||
self.apply_widget_scaling(self.corner_radius),
|
||||
self.apply_widget_scaling(self.border_spacing),
|
||||
corrected_start_value,
|
||||
corrected_end_value,
|
||||
self.orientation)
|
||||
|
||||
if no_color_updates is False or requires_recoloring:
|
||||
if self.hover_state is True:
|
||||
self.canvas.itemconfig("scrollbar_parts",
|
||||
fill=ThemeManager.single_color(self.scrollbar_hover_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.scrollbar_hover_color, self._appearance_mode))
|
||||
else:
|
||||
self.canvas.itemconfig("scrollbar_parts",
|
||||
fill=ThemeManager.single_color(self.scrollbar_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.scrollbar_color, self._appearance_mode))
|
||||
|
||||
if self.fg_color is None:
|
||||
self.canvas.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
self.canvas.itemconfig("border_parts",
|
||||
fill=ThemeManager.single_color(self.bg_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
else:
|
||||
self.canvas.configure(bg=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
self.canvas.itemconfig("border_parts",
|
||||
fill=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
|
||||
self.canvas.update_idletasks()
|
||||
|
||||
def set(self, start_value: float, end_value: float):
|
||||
self.start_value = float(start_value)
|
||||
self.end_value = float(end_value)
|
||||
self.draw()
|
||||
|
||||
def get(self):
|
||||
return self.start_value, self.end_value
|
||||
|
||||
def configure(self, *args, **kwargs):
|
||||
require_redraw = False # some attribute changes require a call of self.draw() at the end
|
||||
|
||||
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 "fg_color" in kwargs:
|
||||
self.fg_color = kwargs["fg_color"]
|
||||
require_redraw = True
|
||||
del kwargs["fg_color"]
|
||||
|
||||
if "scrollbar_color" in kwargs:
|
||||
self.scrollbar_color = kwargs["scrollbar_color"]
|
||||
require_redraw = True
|
||||
del kwargs["scrollbar_color"]
|
||||
|
||||
if "scrollbar_hover_color" in kwargs:
|
||||
self.scrollbar_hover_color = kwargs["scrollbar_hover_color"]
|
||||
require_redraw = True
|
||||
del kwargs["scrollbar_hover_color"]
|
||||
|
||||
if "command" in kwargs:
|
||||
self.command = kwargs["command"]
|
||||
del kwargs["command"]
|
||||
|
||||
if "corner_radius" in kwargs:
|
||||
self.corner_radius = kwargs["corner_radius"]
|
||||
require_redraw = True
|
||||
del kwargs["corner_radius"]
|
||||
|
||||
if "border_spacing" in kwargs:
|
||||
self.border_spacing = kwargs["border_spacing"]
|
||||
require_redraw = True
|
||||
del kwargs["border_spacing"]
|
||||
|
||||
if "width" in kwargs:
|
||||
self.set_dimensions(width=kwargs["width"])
|
||||
del kwargs["width"]
|
||||
|
||||
if "height" in kwargs:
|
||||
self.set_dimensions(height=kwargs["height"])
|
||||
del kwargs["height"]
|
||||
|
||||
super().configure(*args, **kwargs)
|
||||
|
||||
if require_redraw:
|
||||
self.draw()
|
||||
|
||||
def on_enter(self, event=0):
|
||||
if self.hover is True:
|
||||
self.hover_state = True
|
||||
self.canvas.itemconfig("scrollbar_parts",
|
||||
outline=ThemeManager.single_color(self.scrollbar_hover_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.scrollbar_hover_color, self._appearance_mode))
|
||||
|
||||
def on_leave(self, event=0):
|
||||
self.hover_state = False
|
||||
self.canvas.itemconfig("scrollbar_parts",
|
||||
outline=ThemeManager.single_color(self.scrollbar_color, self._appearance_mode),
|
||||
fill=ThemeManager.single_color(self.scrollbar_color, self._appearance_mode))
|
||||
|
||||
def clicked(self, event):
|
||||
if self.orientation == "vertical":
|
||||
value = ((event.y - self.border_spacing) / (self._current_height - 2 * self.border_spacing)) / self._widget_scaling
|
||||
else:
|
||||
value = ((event.x - self.border_spacing) / (self._current_width - 2 * self.border_spacing)) / self._widget_scaling
|
||||
|
||||
current_scrollbar_length = self.end_value - self.start_value
|
||||
value = max(current_scrollbar_length / 2, min(value, 1 - (current_scrollbar_length / 2)))
|
||||
self.start_value = value - (current_scrollbar_length / 2)
|
||||
self.end_value = value + (current_scrollbar_length / 2)
|
||||
self.draw()
|
||||
|
||||
if self.command is not None:
|
||||
self.command('moveto', self.start_value)
|
||||
|
||||
def mouse_scroll_event(self, event=None):
|
||||
if self.command is not None:
|
||||
if sys.platform.startswith("win"):
|
||||
self.command('scroll', -int(event.delta/40), 'units')
|
||||
else:
|
||||
self.command('scroll', -event.delta, 'units')
|
||||
|
@ -30,6 +30,7 @@ class CTkSlider(CTkBaseClass):
|
||||
command=None,
|
||||
variable=None,
|
||||
orient="horizontal",
|
||||
state="normal",
|
||||
**kwargs):
|
||||
|
||||
# set default dimensions according to orientation
|
||||
@ -60,7 +61,7 @@ class CTkSlider(CTkBaseClass):
|
||||
self.border_width = ThemeManager.theme["shape"]["slider_border_width"] if border_width == "default_theme" else border_width
|
||||
self.button_length = ThemeManager.theme["shape"]["slider_button_length"] if button_length == "default_theme" else button_length
|
||||
self.value = 0.5 # initial value of slider in percent
|
||||
self.orient = orient
|
||||
self.orientation = orient
|
||||
self.hover_state = False
|
||||
self.from_ = from_
|
||||
self.to = to
|
||||
@ -75,6 +76,7 @@ class CTkSlider(CTkBaseClass):
|
||||
self.variable: tkinter.Variable = variable
|
||||
self.variable_callback_blocked = False
|
||||
self.variable_callback_name = None
|
||||
self.state = state
|
||||
|
||||
self.grid_rowconfigure(0, weight=1)
|
||||
self.grid_columnconfigure(0, weight=1)
|
||||
@ -124,16 +126,22 @@ class CTkSlider(CTkBaseClass):
|
||||
super().destroy()
|
||||
|
||||
def set_cursor(self):
|
||||
if Settings.cursor_manipulation_enabled:
|
||||
if self.state == "normal" and Settings.cursor_manipulation_enabled:
|
||||
if sys.platform == "darwin":
|
||||
self.configure(cursor="pointinghand")
|
||||
elif sys.platform.startswith("win"):
|
||||
self.configure(cursor="hand2")
|
||||
|
||||
elif self.state == "disabled" and Settings.cursor_manipulation_enabled:
|
||||
if sys.platform == "darwin":
|
||||
self.configure(cursor="arrow")
|
||||
elif sys.platform.startswith("win"):
|
||||
self.configure(cursor="arrow")
|
||||
|
||||
def draw(self, no_color_updates=False):
|
||||
if self.orient.lower() == "horizontal":
|
||||
if self.orientation.lower() == "horizontal":
|
||||
orientation = "w"
|
||||
elif self.orient.lower() == "vertical":
|
||||
elif self.orientation.lower() == "vertical":
|
||||
orientation = "s"
|
||||
else:
|
||||
orientation = "w"
|
||||
@ -166,41 +174,51 @@ class CTkSlider(CTkBaseClass):
|
||||
self.canvas.itemconfig("progress_parts", fill=ThemeManager.single_color(self.progress_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.progress_color, self._appearance_mode))
|
||||
|
||||
self.canvas.itemconfig("slider_parts", fill=ThemeManager.single_color(self.button_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.button_color, self._appearance_mode))
|
||||
if self.hover_state is True:
|
||||
self.canvas.itemconfig("slider_parts",
|
||||
fill=ThemeManager.single_color(self.button_hover_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.button_hover_color, self._appearance_mode))
|
||||
else:
|
||||
self.canvas.itemconfig("slider_parts",
|
||||
fill=ThemeManager.single_color(self.button_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.button_color, self._appearance_mode))
|
||||
|
||||
def clicked(self, event=None):
|
||||
if self.orient.lower() == "horizontal":
|
||||
self.value = (event.x / self._current_width) / self._widget_scaling
|
||||
else:
|
||||
self.value = 1 - (event.y / self._current_height) / self._widget_scaling
|
||||
if self.state == "normal":
|
||||
if self.orientation.lower() == "horizontal":
|
||||
self.value = (event.x / self._current_width) / self._widget_scaling
|
||||
else:
|
||||
self.value = 1 - (event.y / self._current_height) / self._widget_scaling
|
||||
|
||||
if self.value > 1:
|
||||
self.value = 1
|
||||
if self.value < 0:
|
||||
self.value = 0
|
||||
if self.value > 1:
|
||||
self.value = 1
|
||||
if self.value < 0:
|
||||
self.value = 0
|
||||
|
||||
self.output_value = self.round_to_step_size(self.from_ + (self.value * (self.to - self.from_)))
|
||||
self.value = (self.output_value - self.from_) / (self.to - self.from_)
|
||||
self.output_value = self.round_to_step_size(self.from_ + (self.value * (self.to - self.from_)))
|
||||
self.value = (self.output_value - self.from_) / (self.to - self.from_)
|
||||
|
||||
self.draw(no_color_updates=False)
|
||||
self.draw(no_color_updates=False)
|
||||
|
||||
if self.callback_function is not None:
|
||||
self.callback_function(self.output_value)
|
||||
if self.variable is not None:
|
||||
self.variable_callback_blocked = True
|
||||
self.variable.set(round(self.output_value) if isinstance(self.variable, tkinter.IntVar) else self.output_value)
|
||||
self.variable_callback_blocked = False
|
||||
|
||||
if self.variable is not None:
|
||||
self.variable_callback_blocked = True
|
||||
self.variable.set(round(self.output_value) if isinstance(self.variable, tkinter.IntVar) else self.output_value)
|
||||
self.variable_callback_blocked = False
|
||||
if self.callback_function is not None:
|
||||
self.callback_function(self.output_value)
|
||||
|
||||
def on_enter(self, event=0):
|
||||
self.hover_state = True
|
||||
self.canvas.itemconfig("slider_parts", fill=ThemeManager.single_color(self.button_hover_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.button_hover_color, self._appearance_mode))
|
||||
if self.state == "normal":
|
||||
self.hover_state = True
|
||||
self.canvas.itemconfig("slider_parts",
|
||||
fill=ThemeManager.single_color(self.button_hover_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.button_hover_color, self._appearance_mode))
|
||||
|
||||
def on_leave(self, event=0):
|
||||
self.hover_state = False
|
||||
self.canvas.itemconfig("slider_parts", fill=ThemeManager.single_color(self.button_color, self._appearance_mode),
|
||||
self.canvas.itemconfig("slider_parts",
|
||||
fill=ThemeManager.single_color(self.button_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.button_color, self._appearance_mode))
|
||||
|
||||
def round_to_step_size(self, value):
|
||||
@ -231,9 +249,6 @@ class CTkSlider(CTkBaseClass):
|
||||
|
||||
self.draw(no_color_updates=False)
|
||||
|
||||
if self.callback_function is not None:
|
||||
self.callback_function(self.output_value)
|
||||
|
||||
if self.variable is not None and not from_variable_callback:
|
||||
self.variable_callback_blocked = True
|
||||
self.variable.set(round(self.output_value) if isinstance(self.variable, tkinter.IntVar) else self.output_value)
|
||||
@ -246,6 +261,12 @@ class CTkSlider(CTkBaseClass):
|
||||
def configure(self, *args, **kwargs):
|
||||
require_redraw = False # some attribute changes require a call of self.draw() at the end
|
||||
|
||||
if "state" in kwargs:
|
||||
self.state = kwargs["state"]
|
||||
self.set_cursor()
|
||||
require_redraw = True
|
||||
del kwargs["state"]
|
||||
|
||||
if "fg_color" in kwargs:
|
||||
self.fg_color = kwargs["fg_color"]
|
||||
require_redraw = True
|
||||
|
@ -44,7 +44,7 @@ class CTkSwitch(CTkBaseClass):
|
||||
self.button_color = ThemeManager.theme["color"]["switch_button"] if button_color == "default_theme" else button_color
|
||||
self.button_hover_color = ThemeManager.theme["color"]["switch_button_hover"] if button_hover_color == "default_theme" else button_hover_color
|
||||
self.text_color = ThemeManager.theme["color"]["text"] if text_color == "default_theme" else text_color
|
||||
self.text_color_disabled = ThemeManager.theme["color"]["text_button_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
|
||||
|
||||
# text
|
||||
self.text = text
|
||||
@ -62,11 +62,8 @@ class CTkSwitch(CTkBaseClass):
|
||||
self.onvalue = onvalue
|
||||
self.offvalue = offvalue
|
||||
|
||||
# if self.corner_radius < self.button_corner_radius:
|
||||
# self.corner_radius = self.button_corner_radius
|
||||
|
||||
# callback and control variables
|
||||
self.callback_function = command
|
||||
self.function = command
|
||||
self.variable: tkinter.Variable = variable
|
||||
self.variable_callback_blocked = False
|
||||
self.variable_callback_name = None
|
||||
@ -94,6 +91,19 @@ class CTkSwitch(CTkBaseClass):
|
||||
self.canvas.bind("<Leave>", self.on_leave)
|
||||
self.canvas.bind("<Button-1>", self.toggle)
|
||||
|
||||
self.text_label = tkinter.Label(master=self,
|
||||
bd=0,
|
||||
text=self.text,
|
||||
justify=tkinter.LEFT,
|
||||
font=self.apply_font_scaling(self.text_font),
|
||||
textvariable=self.textvariable)
|
||||
self.text_label.grid(row=0, column=2, padx=0, pady=0, sticky="w")
|
||||
self.text_label["anchor"] = "w"
|
||||
|
||||
self.text_label.bind("<Enter>", self.on_enter)
|
||||
self.text_label.bind("<Leave>", self.on_leave)
|
||||
self.text_label.bind("<Button-1>", self.toggle)
|
||||
|
||||
self.draw() # initial draw
|
||||
self.set_cursor()
|
||||
|
||||
@ -186,22 +196,6 @@ class CTkSwitch(CTkBaseClass):
|
||||
self.canvas.itemconfig("slider_parts", fill=ThemeManager.single_color(self.button_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.button_color, self._appearance_mode))
|
||||
|
||||
if self.text_label is None:
|
||||
self.text_label = tkinter.Label(master=self,
|
||||
bd=0,
|
||||
text=self.text,
|
||||
justify=tkinter.LEFT,
|
||||
font=self.apply_font_scaling(self.text_font))
|
||||
self.text_label.grid(row=0, column=2, padx=0, pady=0, sticky="w")
|
||||
self.text_label["anchor"] = "w"
|
||||
|
||||
self.text_label.bind("<Enter>", self.on_enter)
|
||||
self.text_label.bind("<Leave>", self.on_leave)
|
||||
self.text_label.bind("<Button-1>", self.toggle)
|
||||
|
||||
if self.textvariable is not None:
|
||||
self.text_label.configure(textvariable=self.textvariable)
|
||||
|
||||
if self.state == tkinter.DISABLED:
|
||||
self.text_label.configure(fg=(ThemeManager.single_color(self.text_color_disabled, self._appearance_mode)))
|
||||
else:
|
||||
@ -209,13 +203,6 @@ class CTkSwitch(CTkBaseClass):
|
||||
|
||||
self.text_label.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
|
||||
self.set_text(self.text)
|
||||
|
||||
def set_text(self, text):
|
||||
self.text = text
|
||||
if self.text_label is not None:
|
||||
self.text_label.configure(text=self.text)
|
||||
|
||||
def toggle(self, event=None):
|
||||
if self.state is not tkinter.DISABLED:
|
||||
if self.check_state is True:
|
||||
@ -225,8 +212,8 @@ class CTkSwitch(CTkBaseClass):
|
||||
|
||||
self.draw(no_color_updates=True)
|
||||
|
||||
if self.callback_function is not None:
|
||||
self.callback_function()
|
||||
if self.function is not None:
|
||||
self.function()
|
||||
|
||||
if self.variable is not None:
|
||||
self.variable_callback_blocked = True
|
||||
@ -244,8 +231,8 @@ class CTkSwitch(CTkBaseClass):
|
||||
self.variable.set(self.onvalue)
|
||||
self.variable_callback_blocked = False
|
||||
|
||||
if self.callback_function is not None:
|
||||
self.callback_function()
|
||||
if self.function is not None:
|
||||
self.function()
|
||||
|
||||
def deselect(self, from_variable_callback=False):
|
||||
if self.state is not tkinter.DISABLED or from_variable_callback:
|
||||
@ -258,8 +245,8 @@ class CTkSwitch(CTkBaseClass):
|
||||
self.variable.set(self.offvalue)
|
||||
self.variable_callback_blocked = False
|
||||
|
||||
if self.callback_function is not None:
|
||||
self.callback_function()
|
||||
if self.function is not None:
|
||||
self.function()
|
||||
|
||||
def get(self):
|
||||
return self.onvalue if self.check_state is True else self.offvalue
|
||||
@ -286,66 +273,63 @@ class CTkSwitch(CTkBaseClass):
|
||||
def configure(self, *args, **kwargs):
|
||||
require_redraw = False # some attribute changes require a call of self.draw() at the end
|
||||
|
||||
if "text" in kwargs:
|
||||
self.text = kwargs.pop("text")
|
||||
self.text_label.configure(text=self.text)
|
||||
|
||||
if "state" in kwargs:
|
||||
self.state = kwargs["state"]
|
||||
self.state = kwargs.pop("state")
|
||||
self.set_cursor()
|
||||
require_redraw = True
|
||||
del kwargs["state"]
|
||||
|
||||
if "fg_color" in kwargs:
|
||||
self.fg_color = kwargs["fg_color"]
|
||||
self.fg_color = kwargs.pop("fg_color")
|
||||
require_redraw = True
|
||||
del kwargs["fg_color"]
|
||||
|
||||
if "bg_color" in kwargs:
|
||||
if kwargs["bg_color"] is None:
|
||||
new_bg_color = kwargs.pop("bg_color")
|
||||
if new_bg_color is None:
|
||||
self.bg_color = self.detect_color_of_master()
|
||||
else:
|
||||
self.bg_color = kwargs["bg_color"]
|
||||
self.bg_color = new_bg_color
|
||||
require_redraw = True
|
||||
del kwargs["bg_color"]
|
||||
|
||||
if "progress_color" in kwargs:
|
||||
if kwargs["progress_color"] is None:
|
||||
new_progress_color = kwargs.pop("progress_color")
|
||||
if new_progress_color is None:
|
||||
self.progress_color = self.fg_color
|
||||
else:
|
||||
self.progress_color = kwargs["progress_color"]
|
||||
self.progress_color = new_progress_color
|
||||
require_redraw = True
|
||||
del kwargs["progress_color"]
|
||||
|
||||
if "button_color" in kwargs:
|
||||
self.button_color = kwargs["button_color"]
|
||||
self.button_color = kwargs.pop("button_color")
|
||||
require_redraw = True
|
||||
del kwargs["button_color"]
|
||||
|
||||
if "button_hover_color" in kwargs:
|
||||
self.button_hover_color = kwargs["button_hover_color"]
|
||||
self.button_hover_color = kwargs.pop("button_hover_color")
|
||||
require_redraw = True
|
||||
del kwargs["button_hover_color"]
|
||||
|
||||
if "border_color" in kwargs:
|
||||
self.border_color = kwargs["border_color"]
|
||||
self.border_color = kwargs.pop("border_color")
|
||||
require_redraw = True
|
||||
del kwargs["border_color"]
|
||||
|
||||
if "border_width" in kwargs:
|
||||
self.border_width = kwargs["border_width"]
|
||||
self.border_width = kwargs.pop("border_width")
|
||||
require_redraw = True
|
||||
del kwargs["border_width"]
|
||||
|
||||
if "command" in kwargs:
|
||||
self.callback_function = kwargs["command"]
|
||||
del kwargs["command"]
|
||||
self.function = kwargs.pop("command")
|
||||
|
||||
if "textvariable" in kwargs:
|
||||
self.text_label.configure(textvariable=kwargs["textvariable"])
|
||||
del kwargs["textvariable"]
|
||||
self.textvariable = kwargs.pop("textvariable")
|
||||
self.text_label.configure(textvariable=self.textvariable)
|
||||
|
||||
if "variable" in kwargs:
|
||||
if self.variable is not None:
|
||||
self.variable.trace_remove("write", self.variable_callback_name)
|
||||
|
||||
self.variable = kwargs["variable"]
|
||||
self.variable = kwargs.pop("variable")
|
||||
|
||||
if self.variable is not None and self.variable != "":
|
||||
self.variable_callback_name = self.variable.trace_add("write", self.variable_callback)
|
||||
@ -356,8 +340,6 @@ class CTkSwitch(CTkBaseClass):
|
||||
else:
|
||||
self.variable = None
|
||||
|
||||
del kwargs["variable"]
|
||||
|
||||
super().configure(*args, **kwargs)
|
||||
|
||||
if require_redraw:
|
||||
|
163
customtkinter/widgets/ctk_textbox.py
Normal file
163
customtkinter/widgets/ctk_textbox.py
Normal file
@ -0,0 +1,163 @@
|
||||
import tkinter
|
||||
|
||||
from .ctk_canvas import CTkCanvas
|
||||
from ..theme_manager import ThemeManager
|
||||
from ..draw_engine import DrawEngine
|
||||
from .widget_base_class import CTkBaseClass
|
||||
|
||||
|
||||
class CTkTextbox(CTkBaseClass):
|
||||
def __init__(self, *args,
|
||||
bg_color=None,
|
||||
fg_color="default_theme",
|
||||
border_color="default_theme",
|
||||
border_width="default_theme",
|
||||
corner_radius="default_theme",
|
||||
text_font="default_theme",
|
||||
text_color="default_theme",
|
||||
width=200,
|
||||
height=200,
|
||||
**kwargs):
|
||||
|
||||
# transfer basic functionality (bg_color, size, _appearance_mode, scaling) to CTkBaseClass
|
||||
super().__init__(*args, bg_color=bg_color, width=width, height=height)
|
||||
|
||||
# color
|
||||
self.fg_color = ThemeManager.theme["color"]["entry"] if fg_color == "default_theme" else fg_color
|
||||
self.border_color = ThemeManager.theme["color"]["frame_border"] if border_color == "default_theme" else border_color
|
||||
|
||||
# shape
|
||||
self.corner_radius = ThemeManager.theme["shape"]["frame_corner_radius"] if corner_radius == "default_theme" else corner_radius
|
||||
self.border_width = ThemeManager.theme["shape"]["frame_border_width"] if border_width == "default_theme" else border_width
|
||||
|
||||
self.text_color = ThemeManager.theme["color"]["text"] if text_color == "default_theme" else text_color
|
||||
self.text_font = (ThemeManager.theme["text"]["font"], ThemeManager.theme["text"]["size"]) if text_font == "default_theme" else text_font
|
||||
|
||||
# configure 1x1 grid
|
||||
self.grid_rowconfigure(0, weight=1)
|
||||
self.grid_columnconfigure(0, weight=1)
|
||||
|
||||
self.canvas = CTkCanvas(master=self,
|
||||
highlightthickness=0,
|
||||
width=self.apply_widget_scaling(self._current_width),
|
||||
height=self.apply_widget_scaling(self._current_height))
|
||||
self.canvas.grid(row=0, column=0, padx=0, pady=0, rowspan=1, columnspan=1, sticky="nsew")
|
||||
self.canvas.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
self.draw_engine = DrawEngine(self.canvas)
|
||||
|
||||
for arg in ["highlightthickness", "fg", "bg"]:
|
||||
kwargs.pop(arg, None)
|
||||
self.textbox = tkinter.Text(self,
|
||||
fg=ThemeManager.single_color(self.text_color, self._appearance_mode),
|
||||
width=0,
|
||||
height=0,
|
||||
font=self.text_font,
|
||||
highlightthickness=0,
|
||||
bg=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
**kwargs)
|
||||
self.textbox.grid(row=0, column=0, padx=self.corner_radius, pady=self.corner_radius, rowspan=1, columnspan=1, sticky="nsew")
|
||||
|
||||
self.bind('<Configure>', self.update_dimensions_event)
|
||||
|
||||
self.draw()
|
||||
|
||||
def winfo_children(self):
|
||||
""" winfo_children of CTkFrame without self.canvas widget,
|
||||
because it's not a child but part of the CTkFrame itself """
|
||||
|
||||
child_widgets = super().winfo_children()
|
||||
try:
|
||||
child_widgets.remove(self.canvas)
|
||||
return child_widgets
|
||||
except ValueError:
|
||||
return child_widgets
|
||||
|
||||
def set_scaling(self, *args, **kwargs):
|
||||
super().set_scaling(*args, **kwargs)
|
||||
|
||||
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width), height=self.apply_widget_scaling(self._desired_height))
|
||||
self.draw()
|
||||
|
||||
def set_dimensions(self, width=None, height=None):
|
||||
super().set_dimensions(width, height)
|
||||
|
||||
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width),
|
||||
height=self.apply_widget_scaling(self._desired_height))
|
||||
self.draw()
|
||||
|
||||
def draw(self, no_color_updates=False):
|
||||
|
||||
requires_recoloring = self.draw_engine.draw_rounded_rect_with_border(self.apply_widget_scaling(self._current_width),
|
||||
self.apply_widget_scaling(self._current_height),
|
||||
self.apply_widget_scaling(self.corner_radius),
|
||||
self.apply_widget_scaling(self.border_width))
|
||||
|
||||
if no_color_updates is False or requires_recoloring:
|
||||
if self.fg_color is None:
|
||||
self.canvas.itemconfig("inner_parts",
|
||||
fill=ThemeManager.single_color(self.bg_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
else:
|
||||
self.canvas.itemconfig("inner_parts",
|
||||
fill=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
|
||||
self.canvas.itemconfig("border_parts",
|
||||
fill=ThemeManager.single_color(self.border_color, self._appearance_mode),
|
||||
outline=ThemeManager.single_color(self.border_color, self._appearance_mode))
|
||||
self.canvas.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode))
|
||||
|
||||
self.canvas.tag_lower("inner_parts")
|
||||
self.canvas.tag_lower("border_parts")
|
||||
|
||||
def yview(self, args, kwargs):
|
||||
self.textbox.yview(args, kwargs)
|
||||
|
||||
def xview(self, args, kwargs):
|
||||
self.textbox.xview(args, kwargs)
|
||||
|
||||
def insert(self, args, kwargs):
|
||||
self.textbox.insert(args, kwargs)
|
||||
|
||||
def configure(self, *args, **kwargs):
|
||||
require_redraw = False # some attribute changes require a call of self.draw() at the end
|
||||
|
||||
if "fg_color" in kwargs:
|
||||
self.fg_color = kwargs.pop("fg_color")
|
||||
require_redraw = True
|
||||
|
||||
# check if CTk widgets are children of the frame and change their bg_color to new frame fg_color
|
||||
for child in self.winfo_children():
|
||||
if isinstance(child, CTkBaseClass):
|
||||
child.configure(bg_color=self.fg_color)
|
||||
|
||||
if "bg_color" in kwargs:
|
||||
new_bg_color = kwargs.pop("bg_color")
|
||||
if new_bg_color is None:
|
||||
self.bg_color = self.detect_color_of_master()
|
||||
else:
|
||||
self.bg_color = new_bg_color
|
||||
require_redraw = True
|
||||
|
||||
if "border_color" in kwargs:
|
||||
self.border_color = kwargs.pop("border_color")
|
||||
require_redraw = True
|
||||
|
||||
if "corner_radius" in kwargs:
|
||||
self.corner_radius = kwargs.pop("corner_radius")
|
||||
require_redraw = True
|
||||
|
||||
if "border_width" in kwargs:
|
||||
self.border_width = kwargs.pop("border_width")
|
||||
require_redraw = True
|
||||
|
||||
if "width" in kwargs:
|
||||
self.set_dimensions(width=kwargs.pop("width"))
|
||||
|
||||
if "height" in kwargs:
|
||||
self.set_dimensions(height=kwargs.pop("height"))
|
||||
|
||||
self.textbox.configure(*args, **kwargs)
|
||||
|
||||
if require_redraw:
|
||||
self.draw()
|
@ -1,6 +1,7 @@
|
||||
import customtkinter
|
||||
import tkinter
|
||||
import sys
|
||||
import copy
|
||||
import re
|
||||
from typing import Union
|
||||
|
||||
from ..theme_manager import ThemeManager
|
||||
@ -8,20 +9,13 @@ from ..appearance_mode_tracker import AppearanceModeTracker
|
||||
from ..scaling_tracker import ScalingTracker
|
||||
|
||||
|
||||
class DropdownMenu(tkinter.Toplevel):
|
||||
class DropdownMenu(tkinter.Menu):
|
||||
def __init__(self, *args,
|
||||
fg_color="#555555",
|
||||
button_color="gray50",
|
||||
button_hover_color="gray35",
|
||||
text_color="black",
|
||||
corner_radius=6,
|
||||
button_corner_radius=3,
|
||||
width=120,
|
||||
button_height=24,
|
||||
x_position=0,
|
||||
y_position=0,
|
||||
x_spacing=3,
|
||||
y_spacing=3,
|
||||
min_character_width=18,
|
||||
fg_color="default_theme",
|
||||
hover_color="default_theme",
|
||||
text_color="default_theme",
|
||||
text_font="default_theme",
|
||||
command=None,
|
||||
values=None,
|
||||
**kwargs):
|
||||
@ -31,84 +25,105 @@ class DropdownMenu(tkinter.Toplevel):
|
||||
self._widget_scaling = ScalingTracker.get_widget_scaling(self)
|
||||
self._spacing_scaling = ScalingTracker.get_spacing_scaling(self)
|
||||
|
||||
AppearanceModeTracker.add(self.set_appearance_mode, self)
|
||||
self._appearance_mode = AppearanceModeTracker.get_mode() # 0: "Light" 1: "Dark"
|
||||
|
||||
self.min_character_width = min_character_width
|
||||
self.fg_color = ThemeManager.theme["color"]["dropdown_color"] if fg_color == "default_theme" else fg_color
|
||||
self.hover_color = ThemeManager.theme["color"]["dropdown_hover"] if hover_color == "default_theme" else hover_color
|
||||
self.text_color = ThemeManager.theme["color"]["text"] if text_color == "default_theme" else text_color
|
||||
self.text_font = (ThemeManager.theme["text"]["font"], ThemeManager.theme["text"]["size"]) if text_font == "default_theme" else text_font
|
||||
|
||||
self.configure_menu_for_platforms()
|
||||
|
||||
self.values = values
|
||||
self.command = command
|
||||
|
||||
# color
|
||||
self.appearance_mode = AppearanceModeTracker.get_mode() # 0: "Light" 1: "Dark"
|
||||
self.fg_color = fg_color
|
||||
self.button_color = button_color
|
||||
self.button_hover_color = button_hover_color
|
||||
self.text_color = text_color
|
||||
self.add_menu_commands()
|
||||
|
||||
# shape
|
||||
self.corner_radius = corner_radius
|
||||
self.button_corner_radius = button_corner_radius
|
||||
self.button_height = button_height
|
||||
self.width = width
|
||||
self.height = max(len(self.values), 1) * (self.button_height + self.apply_spacing_scaling(y_spacing)) + self.apply_spacing_scaling(y_spacing)
|
||||
|
||||
self.geometry(f"{round(self.apply_widget_scaling(self.width))}x" +
|
||||
f"{round(self.apply_widget_scaling(self.height))}+" +
|
||||
f"{round(x_position)}+{round(y_position)}")
|
||||
self.grid_columnconfigure(0, weight=1)
|
||||
self.grid_rowconfigure(0, weight=1)
|
||||
|
||||
if sys.platform.startswith("darwin"):
|
||||
self.overrideredirect(True) # remove title-bar
|
||||
self.overrideredirect(False)
|
||||
self.wm_attributes("-transparent", True) # turn off window shadow
|
||||
self.config(bg='systemTransparent') # transparent bg
|
||||
self.frame = customtkinter.CTkFrame(self,
|
||||
border_width=0,
|
||||
width=self.width,
|
||||
corner_radius=self.corner_radius,
|
||||
fg_color=ThemeManager.single_color(self.fg_color, self.appearance_mode))
|
||||
def configure_menu_for_platforms(self):
|
||||
if sys.platform == "darwin":
|
||||
self.configure(tearoff=False,
|
||||
font=self.apply_font_scaling(self.text_font))
|
||||
|
||||
elif sys.platform.startswith("win"):
|
||||
self.overrideredirect(True) # remove title-bar
|
||||
self.configure(bg="#010302")
|
||||
self.wm_attributes("-transparentcolor", "#010302")
|
||||
self.focus()
|
||||
self.frame = customtkinter.CTkFrame(self,
|
||||
border_width=0,
|
||||
width=self.width,
|
||||
corner_radius=self.corner_radius,
|
||||
fg_color=self.fg_color, overwrite_preferred_drawing_method="circle_shapes")
|
||||
self.configure(tearoff=False,
|
||||
relief="flat",
|
||||
activebackground=ThemeManager.single_color(self.hover_color, self._appearance_mode),
|
||||
borderwidth=0,
|
||||
activeborderwidth=self.apply_widget_scaling(4),
|
||||
bg=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
fg=ThemeManager.single_color(self.text_color, self._appearance_mode),
|
||||
activeforeground=ThemeManager.single_color(self.text_color, self._appearance_mode),
|
||||
font=self.apply_font_scaling(self.text_font),
|
||||
cursor="hand2")
|
||||
|
||||
else:
|
||||
self.overrideredirect(True) # remove title-bar
|
||||
self.configure(bg="#010302")
|
||||
self.wm_attributes("-transparentcolor", "#010302")
|
||||
self.frame = customtkinter.CTkFrame(self,
|
||||
border_width=0,
|
||||
width=self.width,
|
||||
corner_radius=self.corner_radius,
|
||||
fg_color=self.fg_color, overwrite_preferred_drawing_method="circle_shapes")
|
||||
self.configure(tearoff=False,
|
||||
relief="flat",
|
||||
activebackground=ThemeManager.single_color(self.hover_color, self._appearance_mode),
|
||||
borderwidth=0,
|
||||
activeborderwidth=0,
|
||||
bg=ThemeManager.single_color(self.fg_color, self._appearance_mode),
|
||||
fg=ThemeManager.single_color(self.text_color, self._appearance_mode),
|
||||
activeforeground=ThemeManager.single_color(self.text_color, self._appearance_mode),
|
||||
font=self.apply_font_scaling(self.text_font))
|
||||
|
||||
self.frame.grid(row=0, column=0, sticky="nsew", rowspan=1)
|
||||
self.frame.grid_rowconfigure(len(self.values) + 1, minsize=self.apply_spacing_scaling(y_spacing)) # add spacing at the bottom
|
||||
self.frame.grid_columnconfigure(0, weight=1)
|
||||
def add_menu_commands(self):
|
||||
if sys.platform.startswith("linux"):
|
||||
for value in self.values:
|
||||
self.add_command(label=" " + value.ljust(self.min_character_width) + " ",
|
||||
command=lambda v=value: self.button_callback(v),
|
||||
compound="left")
|
||||
else:
|
||||
for value in self.values:
|
||||
self.add_command(label=value.ljust(self.min_character_width),
|
||||
command=lambda v=value: self.button_callback(v),
|
||||
compound="left")
|
||||
|
||||
self.button_list = []
|
||||
for index, option in enumerate(self.values):
|
||||
button = customtkinter.CTkButton(self.frame,
|
||||
text=option,
|
||||
height=self.button_height,
|
||||
width=self.width - 2 * self.apply_widget_scaling(x_spacing),
|
||||
fg_color=self.button_color,
|
||||
text_color=self.text_color,
|
||||
hover_color=self.button_hover_color,
|
||||
corner_radius=self.button_corner_radius,
|
||||
command=lambda i=index: self.button_callback(i))
|
||||
button.text_label.configure(anchor="w")
|
||||
button.text_label.grid(row=0, column=0, rowspan=2, columnspan=2, sticky="w")
|
||||
button.grid(row=index, column=0,
|
||||
padx=self.apply_widget_scaling(x_spacing),
|
||||
pady=(self.apply_widget_scaling(y_spacing), 0), sticky="ew")
|
||||
self.button_list.append(button)
|
||||
def open(self, x: Union[int, float], y: Union[int, float]):
|
||||
if sys.platform == "darwin":
|
||||
y += self.apply_widget_scaling(8)
|
||||
else:
|
||||
y += self.apply_widget_scaling(3)
|
||||
|
||||
self.bind("<FocusOut>", self.focus_loss_event)
|
||||
self.frame.canvas.bind("<Button-1>", self.focus_loss_event)
|
||||
if sys.platform == "darwin" or sys.platform.startswith("win"):
|
||||
self.post(int(x), int(y))
|
||||
else: # Linux
|
||||
self.tk_popup(int(x), int(y))
|
||||
|
||||
def button_callback(self, value):
|
||||
if self.command is not None:
|
||||
self.command(value)
|
||||
|
||||
def configure(self, **kwargs):
|
||||
if "values" in kwargs:
|
||||
self.values = kwargs["values"]
|
||||
del kwargs["values"]
|
||||
self.delete(0, "end") # delete all old commands
|
||||
self.add_menu_commands()
|
||||
|
||||
if "fg_color" in kwargs:
|
||||
self.fg_color = kwargs["fg_color"]
|
||||
del kwargs["fg_color"]
|
||||
self.configure(bg=ThemeManager.single_color(self.fg_color, self._appearance_mode))
|
||||
|
||||
if "hover_color" in kwargs:
|
||||
self.hover_color = kwargs["hover_color"]
|
||||
del kwargs["hover_color"]
|
||||
self.configure(activebackground=ThemeManager.single_color(self.hover_color, self._appearance_mode))
|
||||
|
||||
if "text_color" in kwargs:
|
||||
self.text_color = kwargs["text_color"]
|
||||
del kwargs["text_color"]
|
||||
self.configure(fg=ThemeManager.single_color(self.text_color, self._appearance_mode))
|
||||
|
||||
if "text_font" in kwargs:
|
||||
self.text_font = kwargs["text_font"]
|
||||
del kwargs["text_font"]
|
||||
self.configure(font=self.apply_font_scaling(self.text_font))
|
||||
|
||||
super().configure(**kwargs)
|
||||
|
||||
def apply_widget_scaling(self, value: Union[int, float, str]) -> Union[float, str]:
|
||||
if isinstance(value, (int, float)):
|
||||
@ -116,24 +131,43 @@ class DropdownMenu(tkinter.Toplevel):
|
||||
else:
|
||||
return value
|
||||
|
||||
def apply_spacing_scaling(self, value: Union[int, float, str]) -> Union[float, str]:
|
||||
if isinstance(value, (int, float)):
|
||||
return value * self._spacing_scaling
|
||||
def apply_font_scaling(self, font):
|
||||
if type(font) == tuple or type(font) == list:
|
||||
font_list = list(font)
|
||||
for i in range(len(font_list)):
|
||||
if (type(font_list[i]) == int or type(font_list[i]) == float) and font_list[i] < 0:
|
||||
font_list[i] = int(font_list[i] * self._widget_scaling)
|
||||
return tuple(font_list)
|
||||
|
||||
elif type(font) == str:
|
||||
for negative_number in re.findall(r" -\d* ", font):
|
||||
font = font.replace(negative_number, f" {int(int(negative_number) * self._widget_scaling)} ")
|
||||
return font
|
||||
|
||||
elif isinstance(font, tkinter.font.Font):
|
||||
new_font_object = copy.copy(font)
|
||||
if font.cget("size") < 0:
|
||||
new_font_object.config(size=int(font.cget("size") * self._widget_scaling))
|
||||
return new_font_object
|
||||
|
||||
else:
|
||||
return value
|
||||
return font
|
||||
|
||||
def set_scaling(self, new_widget_scaling, new_spacing_scaling, new_window_scaling):
|
||||
return
|
||||
self._widget_scaling = new_widget_scaling
|
||||
self._spacing_scaling = new_spacing_scaling
|
||||
|
||||
def focus_loss_event(self, event):
|
||||
self.destroy()
|
||||
if sys.platform.startswith("darwin"):
|
||||
self.update()
|
||||
self.configure(font=self.apply_font_scaling(self.text_font))
|
||||
|
||||
def button_callback(self, index):
|
||||
self.destroy()
|
||||
if sys.platform.startswith("darwin"):
|
||||
self.update()
|
||||
if sys.platform.startswith("win"):
|
||||
self.configure(activeborderwidth=self.apply_widget_scaling(4))
|
||||
|
||||
if self.command is not None:
|
||||
self.command(self.values[index])
|
||||
def set_appearance_mode(self, mode_string):
|
||||
""" colors won't update on appearance mode change when dropdown is open, because it's not necessary """
|
||||
|
||||
if mode_string.lower() == "dark":
|
||||
self._appearance_mode = 1
|
||||
elif mode_string.lower() == "light":
|
||||
self._appearance_mode = 0
|
||||
|
||||
self.configure_menu_for_platforms()
|
||||
|
@ -138,7 +138,7 @@ class CTkBaseClass(tkinter.Frame):
|
||||
self.draw()
|
||||
|
||||
def update_dimensions_event(self, event):
|
||||
# only redraw if dimensions changed (for performance)
|
||||
# only redraw if dimensions changed (for performance), independent of scaling
|
||||
if round(self._current_width) != round(event.width / self._widget_scaling) or round(self._current_height) != round(event.height / self._widget_scaling):
|
||||
self._current_width = (event.width / self._widget_scaling) # adjust current size according to new size given by event
|
||||
self._current_height = (event.height / self._widget_scaling) # _current_width and _current_height are independent of the scale
|
||||
@ -151,7 +151,7 @@ class CTkBaseClass(tkinter.Frame):
|
||||
if master_widget is None:
|
||||
master_widget = self.master
|
||||
|
||||
if isinstance(master_widget, CTkBaseClass) and hasattr(master_widget, "fg_color"): # master is CTkFrame
|
||||
if isinstance(master_widget, (CTkBaseClass, CTk, CTkToplevel)) and hasattr(master_widget, "fg_color"):
|
||||
if master_widget.fg_color is not None:
|
||||
return master_widget.fg_color
|
||||
|
||||
@ -178,11 +178,6 @@ class CTkBaseClass(tkinter.Frame):
|
||||
elif mode_string.lower() == "light":
|
||||
self._appearance_mode = 0
|
||||
|
||||
if isinstance(self.master, (CTkBaseClass, CTk)) and hasattr(self.master, "fg_color"):
|
||||
self.bg_color = self.master.fg_color
|
||||
else:
|
||||
self.bg_color = self.master.cget("bg")
|
||||
|
||||
self.draw()
|
||||
|
||||
def set_scaling(self, new_widget_scaling, new_spacing_scaling, new_window_scaling):
|
||||
|
Reference in New Issue
Block a user