Files

58 lines
2.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import customtkinter as ctk
class ViewBase(ctk.CTkFrame):
def __init__(self, master, app=None, engine=None, state=None, **kwargs):
super().__init__(master, **kwargs)
self.app = app
self.engine = engine
# Не используем self.state — у tkinter/CTk виджетов есть метод state()
self.app_state = state
self.state = state # совместимость со старым кодом
self.title_text = "View"
self._alive = True
self._after_jobs = set()
def after(self, ms, func=None, *args):
"""after() с автоматической очисткой при destroy (чтобы не было «invalid command name»)."""
if not getattr(self, "_alive", True):
return None
holder = {}
def wrapper(*a):
self._after_jobs.discard(holder.get("id"))
if not getattr(self, "_alive", True):
return None
return func(*a) if func is not None else None
job = super().after(ms, wrapper, *args)
holder["id"] = job
self._after_jobs.add(job)
return job
def destroy(self):
self._alive = False
for job in list(getattr(self, "_after_jobs", [])):
try:
super().after_cancel(job)
except Exception:
pass
self._after_jobs.clear()
super().destroy()
def get_title(self):
return self.title_text
def on_show(self):
"""Вызывается при показе вкладки. Переопределяется во вьюхах при необходимости."""
pass
def ui(self, fn):
"""Выполнить fn в UI-потоке (безопасно вызывать из фоновых потоков)."""
app = self.app
if app is not None and hasattr(app, "post_ui"):
app.post_ui(fn)
return
try:
self.after(0, fn)
except Exception:
pass