Skip to content

Commit

Permalink
feat: Add the ability to use the with statement to the containers
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaokang2022 committed Jan 7, 2025
1 parent 1dce00e commit fb89935
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions tkintertools/core/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"Canvas",
]

import abc
import collections.abc
import functools
import platform
Expand All @@ -29,7 +30,21 @@
from . import configs, virtual


class Tk(tkinter.Tk):
class Misc(abc.ABC):
"""An abstract miscellaneous class that implements some details."""

@abc.abstractmethod
def destroy(self) -> None:
"""Destroy the object."""

def __enter__(self) -> typing_extensions.Self:
return self

def __exit__(self, *args, **kwargs) -> None:
self.destroy()


class Tk(tkinter.Tk, Misc):
"""Main window.
In general, there is only one main window. But after destroying it, another
Expand Down Expand Up @@ -285,7 +300,7 @@ def wrapper() -> None:
self.wm_protocol("WM_DELETE_WINDOW", wrapper)


class Toplevel(tkinter.Toplevel, Tk):
class Toplevel(tkinter.Toplevel, Tk, Misc):
"""Toplevel window.
It can be used as a pop-up window, or it can be customized to put anything
Expand Down Expand Up @@ -330,7 +345,7 @@ def destroy(self) -> None:
return tkinter.Toplevel.destroy(self)


class Canvas(tkinter.Canvas):
class Canvas(tkinter.Canvas, Misc):
"""Main contrainer: Canvas.
The parent widget of all virtual widgets is `Canvas`.
Expand Down

0 comments on commit fb89935

Please sign in to comment.