module
This commit is contained in:
94
frames/CTkColoredConsoleFrame.py
Normal file
94
frames/CTkColoredConsoleFrame.py
Normal file
@@ -0,0 +1,94 @@
|
||||
import tkinter as tk
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
|
||||
from customtkinter import CTkTextbox, CTk
|
||||
from loguru import logger
|
||||
|
||||
from gui.utils import fonts
|
||||
|
||||
class LogLevels(Enum):
|
||||
DEBUG = 0
|
||||
ERROR = 1
|
||||
WARNING = 2
|
||||
INFO = 3
|
||||
SUCCESS = 4
|
||||
|
||||
class CTkColoredConsoleFrame(CTkTextbox):
|
||||
|
||||
def __init__(self, master: CTk, corner_radius=0, font=None, _callback=None, **kwargs):
|
||||
if font is None:
|
||||
font = fonts.log_font()
|
||||
super().__init__(master, corner_radius=corner_radius, font=font, **kwargs)
|
||||
self.configure(state='disabled')
|
||||
self.__callback = _callback
|
||||
self._create_tags()
|
||||
self._create_menu()
|
||||
|
||||
def _copy(self, _=None):
|
||||
self.clipboard_clear()
|
||||
selected_text = self.get(tk.SEL_FIRST, tk.SEL_LAST).replace("\n\n", "\n")
|
||||
self.clipboard_append(selected_text)
|
||||
|
||||
def _open_menu(self, ev):
|
||||
self.menu.post(ev.x_root, ev.y_root)
|
||||
|
||||
def _create_menu(self):
|
||||
self.menu = tk.Menu(self, tearoff=0)
|
||||
self.menu.add_command(label="Copy", command=self._copy)
|
||||
self.master.bind("<Control-c>", self._copy)
|
||||
self.master.bind("<Button-3>", self._open_menu)
|
||||
|
||||
def _create_tags(self):
|
||||
self.tag_config(LogLevels.DEBUG, foreground='#0000FF')
|
||||
self.tag_config(LogLevels.ERROR, foreground='#FF0000')
|
||||
self.tag_config(LogLevels.WARNING, foreground='#FFA500')
|
||||
self.tag_config(LogLevels.INFO, foreground='white')
|
||||
self.tag_config(LogLevels.SUCCESS, foreground='#008000')
|
||||
|
||||
def clear(self):
|
||||
self.configure(state='normal')
|
||||
self.delete(1.0, 'end')
|
||||
self.configure(state='disabled')
|
||||
|
||||
def log(self, level, message, _date=None):
|
||||
logger.debug(f"[{self.master}] [{level.name}] {message}")
|
||||
if not _date:
|
||||
_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
if self.__callback:
|
||||
self.__callback(level, message, _date)
|
||||
self.update_idletasks()
|
||||
self.configure(state='normal')
|
||||
self.insert('end', f"{_date} | {level.name} | {message}" + '\n', (level, ))
|
||||
self.see('end')
|
||||
self.configure(state='disabled')
|
||||
self.update_idletasks()
|
||||
|
||||
def debug(self, message):
|
||||
self.after(50, self.log, LogLevels.DEBUG, message)
|
||||
|
||||
def error(self, message):
|
||||
self.after(50, self.log, LogLevels.ERROR, message)
|
||||
|
||||
def warning(self, message):
|
||||
self.after(50, self.log, LogLevels.WARNING, message)
|
||||
|
||||
def info(self, message):
|
||||
self.after(50, self.log, LogLevels.INFO, message)
|
||||
|
||||
def success(self, message):
|
||||
self.after(50, self.log, LogLevels.SUCCESS, message)
|
||||
|
||||
if __name__ == '__main__':
|
||||
_root = CTk()
|
||||
_root.title("CTkColoredConsoleFrame")
|
||||
_root.geometry("400x300")
|
||||
_root.configure(bg='white')
|
||||
con = CTkColoredConsoleFrame(_root)
|
||||
con.pack(fill='both', expand=True)
|
||||
con.debug("Debug message")
|
||||
con.error("Error message")
|
||||
con.warning("Warning message")
|
||||
con.info("Info message")
|
||||
con.success("Success message")
|
||||
_root.mainloop()
|
||||
141
frames/CTkSidebarFrame.py
Normal file
141
frames/CTkSidebarFrame.py
Normal file
@@ -0,0 +1,141 @@
|
||||
from PIL import Image
|
||||
from customtkinter import CTkFrame, CTk, CTkScrollableFrame, CTkLabel, CTkButton, CTkImage
|
||||
from loguru import logger
|
||||
|
||||
from gui.utils import fonts
|
||||
|
||||
|
||||
class CTkSidebarFrame(CTkFrame):
|
||||
fg_color = "#2b2b2b"
|
||||
bg_color = "#333333"
|
||||
|
||||
butt = {
|
||||
"enabled": "#ededed",
|
||||
"disabled": "#363636",
|
||||
"fg": "#3a6069",
|
||||
"bg": "transparent",
|
||||
"hv": "#518894",
|
||||
"pressed": "#2b2b2b"
|
||||
}
|
||||
|
||||
def __init__(self, master: CTk, name, icon_light, icon_dark, name_font=None,
|
||||
options=False, options_text="Options", options_img=None, options_command=None,
|
||||
corner_radius=0, *args, **kwargs):
|
||||
super().__init__(master, corner_radius=corner_radius, *args, **kwargs)
|
||||
|
||||
self.__state = {
|
||||
"status": {
|
||||
"text": "Starting..",
|
||||
"color": "#aec6cf"
|
||||
}
|
||||
}
|
||||
self.__buttons = {}
|
||||
self.active = None
|
||||
|
||||
self._name = name
|
||||
self._name_font = name_font or fonts.heading_font()
|
||||
self._icon_light = icon_light
|
||||
self._icon_dark = icon_dark
|
||||
|
||||
self._add_options = options
|
||||
self._options_text = options_text
|
||||
if options_img is not None:
|
||||
options_img = Image.open(options_img)
|
||||
self._options_img = options_img
|
||||
if options and options_command is None:
|
||||
raise ValueError("options_command must be provided if options is True")
|
||||
self._options_command = options_command
|
||||
|
||||
self.grid_columnconfigure(0, weight=0) # label
|
||||
self.grid_columnconfigure(1, weight=0) # dropdown
|
||||
self.grid_rowconfigure(2, weight=1) # buttons
|
||||
self.grid_rowconfigure(3, weight=0) # settings
|
||||
|
||||
self._create_widgets()
|
||||
|
||||
def _create_widgets(self):
|
||||
self.update_idletasks()
|
||||
|
||||
name_label = CTkLabel(self, text=self._name, fg_color=self.fg_color, bg_color=self.bg_color, corner_radius=0, font=self._name_font)
|
||||
name_label.grid(row=0, column=0, columnspan=2, sticky="ew", padx=5, pady=(10, 0))
|
||||
|
||||
self.status_label = CTkLabel(self, text="Status", fg_color=self.fg_color, bg_color=self.bg_color, corner_radius=0)
|
||||
self.status_label.grid(row=1, column=0, columnspan=2, sticky="ew", padx=5, pady=(0, 3))
|
||||
self._render_status()
|
||||
|
||||
self._sc_frame = CTkScrollableFrame(self, width=160, fg_color=self.fg_color, scrollbar_button_color=self.bg_color, corner_radius=0)
|
||||
self._sc_frame.grid(row=2, column=0, sticky="nsew", padx=5, pady=(3, 10))
|
||||
|
||||
if self._add_options:
|
||||
settings_img = None
|
||||
if self._options_img is not None:
|
||||
settings_img = CTkImage(self._options_img, self._options_img, (12, 12))
|
||||
self.options = CTkButton(self,
|
||||
text=self._options_text, fg_color=self.fg_color, bg_color=self.bg_color,
|
||||
font=fonts.button_med_font(), image=settings_img, command=self._options_command)
|
||||
self.options.grid(row=3, column=0, sticky="ew", padx=5, pady=(0, 10))
|
||||
|
||||
def _render_status(self):
|
||||
status = self.__state["status"]
|
||||
self.status_label.configure(text=status["text"], font=fonts.subheading_font(), text_color=status["color"])
|
||||
|
||||
def _on_press(self, name):
|
||||
logger.debug(f"[{name!r}] pressed")
|
||||
button = self.__buttons[name]
|
||||
if not button['active']:
|
||||
logger.debug(f"{name} is disabled")
|
||||
return
|
||||
self.set_enabled(self.active)
|
||||
self.set_pressed(name)
|
||||
self.active = name
|
||||
button['button'].configure(hover_color=self.butt['pressed'])
|
||||
f = button['command']
|
||||
f_args = button['command_args']
|
||||
f(*f_args)
|
||||
|
||||
def set_status(self, text, color=None):
|
||||
self.__state["status"]["text"] = text
|
||||
if color is not None:
|
||||
self.__state["status"]["color"] = color
|
||||
self._render_status()
|
||||
|
||||
def add_button(self, name, text, command, command_args=(), icon=None):
|
||||
self.update_idletasks()
|
||||
if icon is not None:
|
||||
_icon = Image.open(icon)
|
||||
icon = CTkImage(_icon, _icon, (12, 12))
|
||||
button = CTkButton(self._sc_frame, text=text, font=fonts.button_med_font(), image=icon, command=lambda: self._on_press(name))
|
||||
button.pack(fill="x", padx=5, pady=5)
|
||||
self.__buttons[name] = {"active": True, "button": button, "command": command, "command_args": command_args}
|
||||
self.set_enabled(name)
|
||||
|
||||
def set_pressed(self, name):
|
||||
if name is None:
|
||||
return
|
||||
butt = self.__buttons[name]
|
||||
if not butt['active']:
|
||||
return
|
||||
butt['button'].configure(fg_color=self.butt['pressed'], hover_color=self.butt['pressed'])
|
||||
|
||||
def set_enabled(self, name):
|
||||
self.update_idletasks()
|
||||
logger.debug(f"[{name!r}] set_enabled")
|
||||
if name is None:
|
||||
return
|
||||
butt = self.__buttons[name]
|
||||
butt['active'] = True
|
||||
butt['button'].configure(
|
||||
fg_color=self.butt['fg'], bg_color=self.butt['bg'], text_color=self.butt['enabled'], hover_color=self.butt['hv']
|
||||
)
|
||||
|
||||
def set_disabled(self, name):
|
||||
self.update_idletasks()
|
||||
logger.debug(f"[{name!r}] set_disabled")
|
||||
if name is None:
|
||||
return
|
||||
butt = self.__buttons[name]
|
||||
butt['active'] = False
|
||||
butt['button'].configure(
|
||||
fg_color=self.butt['fg'], bg_color=self.butt['bg'], text_color=self.butt['disabled'],
|
||||
hover_color=self.butt['fg']
|
||||
)
|
||||
82
frames/CTkTableFrame.py
Normal file
82
frames/CTkTableFrame.py
Normal file
@@ -0,0 +1,82 @@
|
||||
import customtkinter as ctk
|
||||
|
||||
from gui.utils import fonts
|
||||
|
||||
|
||||
class CTkTableFrame(ctk.CTkFrame):
|
||||
def __init__(self, master: ctk.CTk, columns, data, callback, width=400, height=200, *args, **kwargs):
|
||||
super().__init__(master, *args, **kwargs)
|
||||
self.columns = columns
|
||||
self.data = data
|
||||
|
||||
self._callback = callback
|
||||
|
||||
self.configure(width=width, height=height)
|
||||
|
||||
self.scroll_frame = ctk.CTkScrollableFrame(self)
|
||||
self.scroll_frame.pack(fill="both", expand=True, padx=5, pady=5)
|
||||
|
||||
self._create_table()
|
||||
|
||||
def _create_table(self):
|
||||
# Заголовки
|
||||
header_frame = ctk.CTkFrame(self.scroll_frame, fg_color="gray30")
|
||||
header_frame.pack(fill="x", padx=2, pady=1)
|
||||
|
||||
for col in self.columns:
|
||||
header_label = ctk.CTkLabel(header_frame, text=col, font=fonts.body_med_font(), padx=5)
|
||||
header_label.pack(side="left", padx=5, pady=3, expand=True)
|
||||
|
||||
# Данные
|
||||
for row_data in self.data:
|
||||
row_frame = ctk.CTkFrame(self.scroll_frame, fg_color="gray20", corner_radius=3, height=20)
|
||||
row_frame.pack(fill="x", padx=2, pady=1)
|
||||
|
||||
# Обработчик двойного клика
|
||||
row_frame.bind("<Double-1>", lambda event, r=row_data: self._callback(r))
|
||||
|
||||
for cell in row_data:
|
||||
cell_label = ctk.CTkLabel(row_frame, text=cell, font=fonts.small_font(), padx=5)
|
||||
cell_label.pack(side="left", padx=5, pady=2, expand=True)
|
||||
|
||||
# Прокидываем клик от ячеек на строку
|
||||
cell_label.bind("<Double-1>", lambda event, r=row_data: self._callback(r))
|
||||
|
||||
|
||||
# Пример использования
|
||||
if __name__ == "__main__":
|
||||
|
||||
app = ctk.CTk()
|
||||
app.geometry("600x400")
|
||||
|
||||
def show_modal(row_data):
|
||||
modal = ctk.CTkToplevel(app)
|
||||
modal.geometry("250x150")
|
||||
modal.title("Детали")
|
||||
|
||||
# Делаем модальное окно поверх основного
|
||||
modal.grab_set()
|
||||
modal.focus_force()
|
||||
|
||||
label_text = f"{row_data}"
|
||||
label = ctk.CTkLabel(modal, text=label_text, font=("Arial", 12))
|
||||
label.pack(pady=15)
|
||||
|
||||
close_btn = ctk.CTkButton(modal, text="Закрыть", command=modal.destroy)
|
||||
close_btn.pack(pady=10)
|
||||
|
||||
ctk.set_appearance_mode("dark")
|
||||
|
||||
columns = ["ID", "Имя", "Логин"]
|
||||
data = [(i, f"Имя {i}", f"i1111-{i}") for i in range(1, 21)] # 20 строк для теста скролла
|
||||
|
||||
lable = ctk.CTkLabel(app, text="Тестовая таблица", font=fonts.title_font())
|
||||
lable.pack(pady=10)
|
||||
|
||||
table = CTkTableFrame(app, columns, data, show_modal, width=400, height=200)
|
||||
table.pack(pady=10, padx=20, fill="both", expand=True)
|
||||
|
||||
but1 = ctk.CTkButton(app, text="Закрыть", command=app.destroy)
|
||||
but1.pack(pady=10)
|
||||
|
||||
app.mainloop()
|
||||
3
frames/__init__.py
Normal file
3
frames/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .CTkSidebarFrame import CTkSidebarFrame
|
||||
from .CTkColoredConsoleFrame import CTkColoredConsoleFrame
|
||||
from .CTkTableFrame import CTkTableFrame
|
||||
Reference in New Issue
Block a user