跳转至

maliang.toolbox.utility

字数 366 个   代码 57 行   阅读时间 2 分钟   访问量

源代码:maliang/toolbox/utility.py

Some useful utility classes or utility functions.

🟢Trigger

class public | object

1
2
3
4
def __init__(
    self,
    command: collections.abc.Callable[..., typing.Any],
) -> None: ...
Single trigger.

It can only be triggered once before the reset, and multiple triggers are invalid. When triggered, the callback function is called.

  • command: the function that is called when triggered

🟡get

method public

1
2
3
def get(
    self,
) -> bool: ...
Get the status of the trigger.

🟡lock

method public

1
2
3
def lock(
    self,
) -> None: ...
Lock the trigger so that it can’t be updated.

🟡reset

method public

1
2
3
def reset(
    self,
) -> None: ...
Reset the status of the trigger.

🟡unlock

method public

1
2
3
def unlock(
    self,
) -> None: ...
Unlock this trigger so that it can be updated again.

🟡update

method public

1
2
3
4
5
6
7
def update(
    self,
    value: bool = True,
    /,
    *args,
    **kwargs,
) -> None: ...
Update the status of the trigger.

  • value: updated value
  • args: args of the command
  • kwargs: kwargs of the command

🔵create_smoke

function public

1
2
3
4
5
def create_smoke(
    size: tuple[int, int],
    *,
    color: str | tuple[int, int, int, int] = '#00000066',
) -> enhanced.PhotoImage: ...
Create a temporary smoke zone. Return the enhanced.PhotoImage.

  • size: size of the smoke zone
  • color: color of the smoke zone

This function need PIL to run.

About the “smoke”, see: https://fluent2.microsoft.design/material#smoke

🔵embed_window

function public

1
2
3
4
5
6
def embed_window(
    window: tkinter.Misc,
    parent: tkinter.Misc | None,
    *,
    focus: bool = False,
) -> None: ...
Embed a widget into another widget.

  • window: Widget that will be embedded in
  • parent: parent widget, None indicates that the parent widget is the screen
  • focus: whether direct input focus to this window

🔵fix_cursor

function public

1
2
3
def fix_cursor(
    name: str,
) -> str: ...
Fix the cursor name.

  • name: name of cursor

🔵get_parent

function public

1
2
3
def get_parent(
    widget: tkinter.Misc,
) -> int: ...
Get the HWND of tkinter.Widget.

  • widget: the widget

🔵get_text_size

function public

1
2
3
4
5
6
7
8
9
def get_text_size(
    text: str,
    fontsize: int | None = None,
    family: str | None = None,
    *,
    padding: int = 0,
    master: tkinter.Canvas | virtual.Widget | None = None,
    **kwargs,
) -> tuple[int, int]: ...
Get the size of a text with a special font family and font size.

  • text: the text
  • fontsize: font size of the text
  • family: font family of the text
  • padding: extra padding of the size
  • master: default canvas or widget provided
  • kwargs: kwargs of tkinter.font.Font

This function only works when the fontsize is negative number!

🔵load_font

function public

1
2
3
4
5
6
def load_font(
    font_path: str | bytes,
    *,
    private: bool = True,
    enumerable: bool = False,
) -> bool: ...
Make fonts located in file font_path available to the font system, and return True if the operation succeeds, False otherwise.

  • font_path: the font file path
  • private: if True, other processes cannot see this font(Only Windows OS), and this font will be unloaded when the process dies
  • enumerable: if True, this font will appear when enumerating fonts(Only Windows OS)

This function only works on Windows and Linux OS.

This function is referenced from customtkinter.FontManager.load_font, CustomTkinter: TomSchimansky/CustomTkinter.

🔵screen_size

function public

def screen_size(
) -> tuple[int, int]: ...
Returns the size of the screen.