Compare commits

...

2 Commits

Author SHA1 Message Date
2f389c8d0e [~] Optimize render 2025-03-27 13:13:25 +03:00
cf62a0e750 [~] FIX width 2025-03-26 18:29:34 +03:00
2 changed files with 49 additions and 35 deletions

View File

@@ -26,8 +26,8 @@ class CTkMessageBox(ctk.CTkToplevel):
},
"error": {"Ok": {"output": True, "row": 0, "column": 0, "sticky": "center"}},
"yesno": {
"Yes": {"output": True, "row": 0, "column": 0, "sticky": "e"},
"No": {"output": False, "row": 0, "column": 1, "sticky": "w"}
"Yes": {"output": True, "row": 0, "width": 60, "padx": (0, 75)},
"No": {"output": False, "row": 0, "width": 60, "padx": (0, 0)}
}
}
def __init__(self,
@@ -53,6 +53,8 @@ class CTkMessageBox(ctk.CTkToplevel):
buttons: Dict[str, Any] | str = 'auto'):
super().__init__(fg_color=fg_color)
self.resizable(False, False)
self.withdraw()
self._fg_color = ThemeManager.theme["CTkToplevel"]["fg_color"] if fg_color is None else self._check_color_type(fg_color)
self._text_color = ThemeManager.theme["CTkLabel"]["text_color"] if text_color is None else self._check_color_type(button_hover_color)
@@ -73,7 +75,7 @@ class CTkMessageBox(ctk.CTkToplevel):
_text_slt = []
_x = 0
self._x = 0
for line in self._text.split('\n'):
if len(line) * 7 > 700:
wrapped_lines = wrap_text(line, max_message_line_width)
@@ -81,55 +83,52 @@ class CTkMessageBox(ctk.CTkToplevel):
else:
_text_slt.append(line)
_x = max(_x, max(len(w) * 7 for w in _text_slt))
_x += 20 # Add 20 pixels for padding on the left and right
_x += 10 # Add 10 pixels for padding on the left and right
self._x = max(self._x, max(len(w) * 7 for w in _text_slt))
self._x += 20 # Add 20 pixels for padding on the left and right
self._x += 10 # Add 10 pixels for padding on the left and right
_x = max(_x, (len(self.header_map[mode]) * 16) + 25 + 20) # Set width to the length of the title if it's longer than the message
self._x = max(self._x, (len(self.header_map[mode]) * 16) + 25 + 20) # Set width to the length of the title if it's longer than the message
self._text = "\n".join(_text_slt)
_y = 0
_y += 24 + 10 + 5 # Add 24 pixels for the header (icon + text) and 15 pixels for padding
_y += len(_text_slt) * 12 # Calculate height based on the number of lines in self._text
_y += 15 + 10 # Add 25 pixels for padding between the header and the message
self._y = 0
self._y += 24 + 10 + 5 # Add 24 pixels for the header (icon + text) and 15 pixels for padding
self._y += len(_text_slt) * 12 # Calculate height based on the number of lines in self._text
self._y += 15 + 10 # Add 25 pixels for padding between the header and the message
if timeout > 0:
if self._header:
header = self._header(self, self.title(), disable_hide=True, disable_close=True)
header.pack(fill="x", pady=(0, 0))
self.geometry(f"{_x}x{_y}")
self.geometry(f"{self._x}x{self._y}")
self.overrideredirect(True) # remove window decorations
self.after(self._timeout, self.destroy) # close window after timeout
self.after(self._timeout, self._on_closing) # close window after timeout
self._buttons = {}
else:
_y += 30 + 10 + 12 # Add 30 pixels for the buttons
self.geometry(f"{_x}x{_y+12}") # Add 12 pixels to the height for the title bar
self._y += 30 + 10 + 12 # Add 30 pixels for the buttons
self.geometry(f"{self._x}x{self._y+12}") # Add 12 pixels to the height for the title bar
if buttons == "auto":
self._buttons = self.buttons_map.get(self._mode, {})
elif isinstance(buttons, str):
self._buttons = {buttons: {"output": True, "row": 0, "column": 0, "sticky": "center"}}
self.attributes("-topmost", True) # stay on top
self._center()
self.transient(parent)
self.title(title)
self.lift() # lift window on top
self.protocol("WM_DELETE_WINDOW", self._on_closing)
self._create_widgets()
self.resizable(False, False)
self._set_on_pos()
self.grab_set() # make other windows not clickable
def _set_on_pos(self):
self.update_idletasks()
y = (self.winfo_screenheight() - self.winfo_reqheight()) // 8
self.geometry(f"+{y}+{y}")
self.protocol("WM_DELETE_WINDOW", self._on_closing)
def _on_closing(self):
self.withdraw()
self.grab_release()
if self.winfo_exists():
self.destroy()
self.destroy()
def _center(self):
x = (self.winfo_screenwidth() - self._x) // 2
y = (self.winfo_screenheight() - self._y) // 2
self.geometry(f'{x}+{y}')
def _create_widgets(self):
# Иконки для типов сообщений
@@ -166,6 +165,9 @@ class CTkMessageBox(ctk.CTkToplevel):
self._on_closing()
def get_output(self):
self.lift()
self.grab_set()
self.deiconify()
self.master.wait_window(self)
if self._timeout > 0 and not self._input:
return

View File

@@ -2,12 +2,15 @@ import customtkinter as ctk
def darken_color_rgb(hex_color, amount=30):
"""Затемняет цвет, вычитая значение из каждого компонента RGB"""
hex_color = hex_color.lstrip("#")
r, g, b = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
# Уменьшаем компоненты, не давая им уйти в минус
r, g, b = max(0, r - amount), max(0, g - amount), max(0, b - amount)
try:
hex_color = hex_color.lstrip("#")
r, g, b = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
# Уменьшаем компоненты, не давая им уйти в минус
r, g, b = max(0, r - amount), max(0, g - amount), max(0, b - amount)
except Exception as e:
print(e, hex_color)
raise e
return f"#{r:02X}{g:02X}{b:02X}"
class CTkTableFrame(ctk.CTkFrame):
@@ -44,6 +47,7 @@ class CTkTableFrame(ctk.CTkFrame):
def _prepare_columns(self):
# Применяем шаблон значений по умолчанию
default_column = {"width": 0, "align": "left", "name": "N/A"}
self._columns.clear()
for col in self.columns:
if type(col) == dict:
if "width" not in col:
@@ -65,16 +69,22 @@ class CTkTableFrame(ctk.CTkFrame):
def _build_header(self):
"""Создает заголовок таблицы."""
header_frame = ctk.CTkFrame(self, fg_color="gray30", corner_radius=10)
header_frame.pack(fill="x", padx=0, pady=1)
self.header_frame = ctk.CTkFrame(self, fg_color="gray30", corner_radius=10)
self.header_frame.pack(fill="x", padx=0, pady=1)
# Заголовки
for col in self._columns:
header_label = ctk.CTkLabel(
header_frame, text=col["name"], width=col["width"], anchor=col["align"], padx=5
self.header_frame, text=col["name"], width=col["width"], anchor=col["align"], padx=5
)
header_label.pack(side="left", padx=4, pady=3)
def _update_header(self):
"""Обновляет заголовок таблицы."""
for i, col in enumerate(self._columns):
header_label = self.header_frame.winfo_children()[i]
header_label.configure(width=col["width"], anchor=col["align"])
@staticmethod
def __row_enter(frame, e, color="gray40"):
frame.configure(fg_color=color)
@@ -111,6 +121,8 @@ class CTkTableFrame(ctk.CTkFrame):
def create_table(self):
"""Создает таблицу с заголовками и данными, используя параметры из словаря."""
self._prepare_columns()
self._update_header()
self._rows = [None] * len(self._data)
self._rows_settings = [None] * len(self._data)
loading = self.loading_frame.winfo_children()[0]