CustomTkinter/customtkinter/widgets/ctk_optionmenu.py

297 lines
14 KiB
Python
Raw Normal View History

2022-05-25 23:14:38 +03:00
import tkinter
import sys
from .dropdown_menu import DropdownMenu
from .ctk_canvas import CTkCanvas
from ..theme_manager import ThemeManager
from ..settings import Settings
from ..draw_engine import DrawEngine
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",
2022-06-15 02:24:02 +03:00
text_color="default_theme",
text_color_disabled="default_theme",
2022-05-27 01:12:37 +03:00
dropdown_color="default_theme",
dropdown_hover_color="default_theme",
dropdown_text_color="default_theme",
2022-05-25 23:14:38 +03:00
variable=None,
values=None,
command=None,
2022-05-31 23:32:21 +03:00
width=140,
2022-05-25 23:14:38 +03:00
height=28,
corner_radius="default_theme",
text_font="default_theme",
2022-06-15 02:24:02 +03:00
dropdown_text_font="default_theme",
2022-05-25 23:14:38 +03:00
hover=True,
state=tkinter.NORMAL,
2022-06-23 22:05:44 +03:00
dynamic_resizing=True,
2022-05-25 23:14:38 +03:00
**kwargs):
2022-05-31 23:32:21 +03:00
# transfer basic functionality (bg_color, size, _appearance_mode, scaling) to CTkBaseClass
2022-05-25 23:14:38 +03:00
super().__init__(*args, bg_color=bg_color, width=width, height=height, **kwargs)
# color variables
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
# shape
self.corner_radius = ThemeManager.theme["shape"]["button_corner_radius"] if corner_radius == "default_theme" else corner_radius
# text and font
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
2022-06-15 02:24:02 +03:00
self.dropdown_text_font = dropdown_text_font
2022-05-25 23:14:38 +03:00
# callback and hover functionality
self.command = command
2022-05-25 23:14:38 +03:00
self.variable = variable
2022-05-27 01:12:37 +03:00
self.variable_callback_blocked = False
self.variable_callback_name = None
2022-05-25 23:14:38 +03:00
self.state = state
self.hover = hover
2022-06-23 22:05:44 +03:00
self.dynamic_resizing = dynamic_resizing
2022-05-31 23:32:21 +03:00
2022-05-25 23:14:38 +03:00
if values is None:
self.values = ["CTkOptionMenu"]
else:
self.values = values
2022-05-31 23:32:21 +03:00
if len(self.values) > 0:
self.current_value = self.values[0]
else:
self.current_value = "CTkOptionMenu"
2022-06-15 02:24:02 +03:00
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)
2022-05-25 23:14:38 +03:00
# configure grid system (1x1)
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
self.canvas = CTkCanvas(master=self,
highlightthickness=0,
2022-05-31 23:32:21 +03:00
width=self.apply_widget_scaling(self._desired_width),
height=self.apply_widget_scaling(self._desired_height))
2022-05-25 23:14:38 +03:00
self.canvas.grid(row=0, column=0, rowspan=1, columnspan=1, sticky="nsew")
self.draw_engine = DrawEngine(self.canvas)
2022-06-15 02:24:02 +03:00
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)
2022-06-15 02:24:02 +03:00
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))))
2022-06-23 22:05:44 +03:00
if not self.dynamic_resizing:
self.grid_propagate(0)
2022-06-15 00:58:21 +03:00
if Settings.cursor_manipulation_enabled:
if sys.platform == "darwin":
self.configure(cursor="pointinghand")
elif sys.platform.startswith("win"):
self.configure(cursor="hand2")
2022-05-25 23:14:38 +03:00
# 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)
2022-06-15 02:24:02 +03:00
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)
2022-05-25 23:14:38 +03:00
self.bind('<Configure>', self.update_dimensions_event)
self.draw() # initial draw
2022-05-27 01:12:37 +03:00
if self.variable is not None:
self.variable_callback_name = self.variable.trace_add("write", self.variable_callback)
self.set(self.variable.get(), from_variable_callback=True)
2022-05-25 23:14:38 +03:00
def set_scaling(self, *args, **kwargs):
super().set_scaling(*args, **kwargs)
# 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))))
2022-05-25 23:14:38 +03:00
2022-05-31 23:32:21 +03:00
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width),
height=self.apply_widget_scaling(self._desired_height))
2022-05-25 23:14:38 +03:00
self.draw()
def set_dimensions(self, width: int = None, height: int = None):
2022-05-25 23:14:38 +03:00
super().set_dimensions(width, height)
2022-05-31 23:32:21 +03:00
self.canvas.configure(width=self.apply_widget_scaling(self._desired_width),
height=self.apply_widget_scaling(self._desired_height))
2022-05-25 23:14:38 +03:00
self.draw()
def draw(self, no_color_updates=False):
2022-05-31 23:32:21 +03:00
left_section_width = self._current_width - self._current_height
requires_recoloring = self.draw_engine.draw_rounded_rect_with_border_vertical_split(self.apply_widget_scaling(self._current_width),
self.apply_widget_scaling(self._current_height),
2022-05-25 23:14:38 +03:00
self.apply_widget_scaling(self.corner_radius),
0,
self.apply_widget_scaling(left_section_width))
2022-05-31 23:32:21 +03:00
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))
2022-05-25 23:14:38 +03:00
2022-05-27 02:09:54 +03:00
if no_color_updates is False or requires_recoloring or requires_recoloring_2:
2022-05-25 23:14:38 +03:00
2022-05-31 23:32:21 +03:00
self.canvas.configure(bg=ThemeManager.single_color(self.bg_color, self._appearance_mode))
2022-05-25 23:14:38 +03:00
self.canvas.itemconfig("inner_parts_left",
2022-05-31 23:32:21 +03:00
outline=ThemeManager.single_color(self.fg_color, self._appearance_mode),
fill=ThemeManager.single_color(self.fg_color, self._appearance_mode))
2022-05-25 23:14:38 +03:00
self.canvas.itemconfig("inner_parts_right",
2022-05-31 23:32:21 +03:00
outline=ThemeManager.single_color(self.button_color, self._appearance_mode),
fill=ThemeManager.single_color(self.button_color, self._appearance_mode))
2022-05-25 23:14:38 +03:00
2022-05-31 23:32:21 +03:00
self.text_label.configure(fg=ThemeManager.single_color(self.text_color, self._appearance_mode))
2022-05-25 23:14:38 +03:00
if self.state == tkinter.DISABLED:
2022-05-31 23:32:21 +03:00
self.text_label.configure(fg=(ThemeManager.single_color(self.text_color_disabled, self._appearance_mode)))
2022-05-27 02:09:54 +03:00
self.canvas.itemconfig("dropdown_arrow",
2022-05-31 23:32:21 +03:00
fill=ThemeManager.single_color(self.text_color_disabled, self._appearance_mode))
2022-05-25 23:14:38 +03:00
else:
2022-05-31 23:32:21 +03:00
self.text_label.configure(fg=ThemeManager.single_color(self.text_color, self._appearance_mode))
2022-05-27 02:09:54 +03:00
self.canvas.itemconfig("dropdown_arrow",
2022-05-31 23:32:21 +03:00
fill=ThemeManager.single_color(self.text_color, self._appearance_mode))
2022-05-25 23:14:38 +03:00
2022-05-31 23:32:21 +03:00
self.text_label.configure(bg=ThemeManager.single_color(self.fg_color, self._appearance_mode))
2022-05-25 23:14:38 +03:00
2022-06-23 22:05:44 +03:00
self.canvas.update_idletasks()
2022-05-25 23:14:38 +03:00
def open_dropdown_menu(self):
2022-06-15 00:58:21 +03:00
self.dropdown_menu.open(self.winfo_rootx(),
self.winfo_rooty() + self.apply_widget_scaling(self._current_height + 0))
def configure(self, require_redraw=False, **kwargs):
2022-05-25 23:14:38 +03:00
if "state" in kwargs:
2022-06-23 22:05:44 +03:00
self.state = kwargs.pop("state")
2022-05-25 23:14:38 +03:00
require_redraw = True
if "fg_color" in kwargs:
2022-06-23 22:05:44 +03:00
self.fg_color = kwargs.pop("fg_color")
2022-05-25 23:14:38 +03:00
require_redraw = True
if "button_color" in kwargs:
2022-06-23 22:05:44 +03:00
self.button_color = kwargs.pop("button_color")
require_redraw = True
if "button_hover_color" in kwargs:
2022-06-23 22:05:44 +03:00
self.button_hover_color = kwargs.pop("button_hover_color")
2022-05-25 23:14:38 +03:00
require_redraw = True
if "text_color" in kwargs:
2022-06-23 22:05:44 +03:00
self.text_color = kwargs.pop("text_color")
2022-05-25 23:14:38 +03:00
require_redraw = True
if "command" in kwargs:
self.command = kwargs.pop("command")
2022-05-25 23:14:38 +03:00
if "variable" in kwargs:
2022-05-27 01:12:37 +03:00
if self.variable is not None: # remove old callback
self.variable.trace_remove("write", self.variable_callback_name)
2022-06-23 22:05:44 +03:00
self.variable = kwargs.pop("variable")
2022-05-27 01:12:37 +03:00
if self.variable is not None and self.variable != "":
self.variable_callback_name = self.variable.trace_add("write", self.variable_callback)
self.set(self.variable.get(), from_variable_callback=True)
else:
self.variable = None
2022-05-25 23:14:38 +03:00
if "width" in kwargs:
2022-06-23 22:05:44 +03:00
self.set_dimensions(width=kwargs.pop("width"))
2022-05-25 23:14:38 +03:00
if "height" in kwargs:
2022-06-23 22:05:44 +03:00
self.set_dimensions(height=kwargs.pop("height"))
2022-05-25 23:14:38 +03:00
2022-05-31 23:32:21 +03:00
if "values" in kwargs:
2022-06-23 22:05:44 +03:00
self.values = kwargs.pop("values")
2022-06-15 00:58:21 +03:00
self.dropdown_menu.configure(values=self.values)
2022-05-31 23:32:21 +03:00
2022-06-15 02:24:02 +03:00
if "dropdown_color" in kwargs:
2022-06-23 22:05:44 +03:00
self.dropdown_menu.configure(fg_color=kwargs.pop("dropdown_color"))
2022-06-15 02:24:02 +03:00
if "dropdown_hover_color" in kwargs:
2022-06-23 22:05:44 +03:00
self.dropdown_menu.configure(hover_color=kwargs.pop("dropdown_hover_color"))
2022-06-15 02:24:02 +03:00
if "dropdown_text_color" in kwargs:
2022-06-23 22:05:44 +03:00
self.dropdown_menu.configure(text_color=kwargs.pop("dropdown_text_color"))
2022-06-15 02:24:02 +03:00
if "dropdown_text_font" in kwargs:
2022-06-23 22:05:44 +03:00
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)
2022-06-15 02:24:02 +03:00
super().configure(require_redraw=require_redraw, **kwargs)
2022-05-25 23:14:38 +03:00
def on_enter(self, event=0):
2022-06-02 00:50:50 +03:00
if self.hover is True and self.state == tkinter.NORMAL and len(self.values) > 0:
2022-05-25 23:14:38 +03:00
# set color of inner button parts to hover color
self.canvas.itemconfig("inner_parts_right",
2022-05-31 23:32:21 +03:00
outline=ThemeManager.single_color(self.button_hover_color, self._appearance_mode),
fill=ThemeManager.single_color(self.button_hover_color, self._appearance_mode))
2022-05-25 23:14:38 +03:00
def on_leave(self, event=0):
if self.hover is True:
# set color of inner button parts
self.canvas.itemconfig("inner_parts_right",
2022-05-31 23:32:21 +03:00
outline=ThemeManager.single_color(self.button_color, self._appearance_mode),
fill=ThemeManager.single_color(self.button_color, self._appearance_mode))
2022-05-25 23:14:38 +03:00
2022-05-27 01:12:37 +03:00
def variable_callback(self, var_name, index, mode):
if not self.variable_callback_blocked:
self.set(self.variable.get(), from_variable_callback=True)
def set(self, value: str, from_variable_callback: bool = False):
self.current_value = value
self.text_label.configure(text=self.current_value)
2022-05-27 01:12:37 +03:00
if self.variable is not None and not from_variable_callback:
self.variable_callback_blocked = True
self.variable.set(self.current_value)
self.variable_callback_blocked = False
2022-05-30 17:46:36 +03:00
if not from_variable_callback:
if self.command is not None:
self.command(self.current_value)
2022-05-30 17:46:36 +03:00
2022-05-27 01:12:37 +03:00
def get(self) -> str:
return self.current_value
2022-05-25 23:14:38 +03:00
def clicked(self, event=0):
2022-06-02 00:50:50 +03:00
if self.state is not tkinter.DISABLED and len(self.values) > 0:
2022-05-25 23:14:38 +03:00
self.open_dropdown_menu()