跳转至

maliang.core.virtual

字数 9 个   阅读时间不到 1 分钟   访问量

源代码:maliang/core/virtual.py

All virtual classes.

The virtual.Widget consists of five parts, which are Shape, Text, Image, Style and Feature. In addition, they can be nested within each other.

Where Feature is the function of widgets, Style control the color of the widget, and each widget can be bound to up to one Feature and one Style, but in terms of appearance, there is no limit to the number of Shape, Text, and Image.

Shape, Text, and Image are all appearance elements that inherit from abstract base class Elements.

类:

  • Element

    The basic visible part of a virtual.Widget.

  • Shape

    The Shape of a Widget

  • Text

    The Text of a Widget.

  • Image

    The Image of a Widget.

  • Style

    The styles of a Widget.

  • Feature

    The features of a Widget.

  • Widget

    Base Widget Class.

Element

Element(
    widget: Widget,
    position: tuple[int, int] = (0, 0),
    size: tuple[int, int] | None = None,
    *,
    name: str | None = None,
    gradient_animation: bool | None = None,
    **kwargs
)

Bases: abc.ABC

The basic visible part of a virtual.Widget.

  • widget: parent widget
  • position: position relative to its widgets
  • size: size of element
  • name: name of element
  • gradient_animation: Wether use animation to change color
  • kwargs: extra parameters for CanvasItem

方法:

  • move

    Move the Element.

  • moveto

    Move the Element to a certain position.

  • destroy

    Destroy the Element.

  • center

    Return the geometric center of the Element.

  • region

    Return the decision region of the Element.

  • detect

    Detect whether the specified coordinates are within Element.

  • update

    Update the style of the Element to the corresponding state.

  • configure

    Configure properties of Element and update them immediately.

  • forget

    Let the element to forget.

  • zoom

    Zoom the Element.

  • display

    Display the Element on a Canvas.

  • coords

    Resize the Element.

move

move(dx: float, dy: float) -> None

Move the Element.

  • dx: x-coordinate offset
  • dy: y-coordinate offset

moveto

moveto(x: float, y: float) -> None

Move the Element to a certain position.

  • x: x-coordinate of the target location
  • y: y-coordinate of the target location

destroy

destroy() -> None

Destroy the Element.

center

center() -> tuple[float, float]

Return the geometric center of the Element.

region

region() -> tuple[int, int, int, int]

Return the decision region of the Element.

detect

detect(x: float, y: float) -> bool

Detect whether the specified coordinates are within Element.

  • x: x-coordinate of the location to be detected
  • y: y-coordinate of the location to be detected

update

update(state: str | None = None, *, gradient_animation: bool = False) -> None

Update the style of the Element to the corresponding state.

  • state: the state of the Element
  • gradient_animation: whether use gradient animation

configure

configure(style: dict[str, str], *, gradient_animation: bool = True) -> None

Configure properties of Element and update them immediately.

  • style: style data
  • gradient_animation: whether use gradient animation

forget

forget(value: bool = True, *, gradient_animation: bool = False) -> None

Let the element to forget.

  • value: whether to forget
  • gradient_animation: whether use gradient animation

zoom

zoom(ratios: tuple[float, float], *, zoom_position: bool = True, zoom_size: bool = True) -> None

Zoom the Element.

  • ratios: ratios of zooming
  • zoom_position: whether or not to zoom the location of the element
  • zoom_size: whether or not to zoom the size of the element

display abstractmethod

display() -> None

Display the Element on a Canvas.

coords abstractmethod

coords(
    size: tuple[float, float] | None = None, position: tuple[float, float] | None = None
) -> None

Resize the Element.

  • size: new size of the element
  • position: new position of the element

Shape

Shape(
    widget: Widget,
    position: tuple[int, int] = (0, 0),
    size: tuple[int, int] | None = None,
    *,
    name: str | None = None,
    gradient_animation: bool | None = None,
    **kwargs
)

Bases: Element

The Shape of a Widget

方法:

  • zoom

    Scale the shape.

zoom

zoom(ratios: tuple[float, float], *, zoom_position: bool = True, zoom_size: bool = True) -> None

Scale the shape.

  • ratios: ratios of zooming
  • zoom_position: whether or not to zoom the location of the shape
  • zoom_size: whether or not to zoom the size of the shape

Text

Text(
    widget: Widget,
    relative_position: tuple[int, int] = (0, 0),
    size: tuple[int, int] | None = None,
    *,
    text: str = "",
    limit: int = -1,
    show: str | None = None,
    placeholder: str = "",
    family: str | None = None,
    fontsize: int | None = None,
    weight: typing.Literal["normal", "bold"] = "normal",
    slant: typing.Literal["roman", "italic"] = "roman",
    underline: bool = False,
    overstrike: bool = False,
    name: str | None = None,
    gradient_animation: bool = True,
    **kwargs
)

Bases: Element

The Text of a Widget.

  • widget: parent widget
  • relative_position: position relative to its widgets
  • size: size of element
  • text: text value
  • family: font family
  • fontsize: font size
  • weight: weight of the font
  • slant: slant of the font
  • underline: wether text is underline
  • overstrike: wether text is overstrike
  • limit: limit on the number of characters
  • show: display a value that obscures the original content
  • placeholder: a placeholder for the prompt
  • name: name of element
  • gradient_animation: Wether use animation to change color
  • kwargs: extra parameters for CanvasItem

方法:

  • region

    Return the decision region of the Text.

  • zoom

    Scale the text.

region

region() -> tuple[int, int, int, int]

Return the decision region of the Text.

zoom

zoom(ratios: tuple[float, float], *, zoom_position: bool = True, zoom_size: bool = True) -> None

Scale the text.

  • ratios: ratios of zooming
  • zoom_position: whether or not to zoom the location of the text
  • zoom_size: whether or not to zoom the size of the text

Image

Image(
    widget: Widget,
    relative_position: tuple[int, int] = (0, 0),
    size: tuple[int, int] | None = None,
    *,
    image: enhanced.PhotoImage | None = None,
    name: str | None = None,
    gradient_animation: bool = True,
    **kwargs
)

Bases: Element

The Image of a Widget.

  • widget: parent widget
  • relative_position: position relative to its widgets
  • size: size of element
  • image: image object of the element
  • name: name of element
  • gradient_animation: Wether use animation to change color
  • kwargs: extra parameters for CanvasItem

方法:

  • region

    Return the decision region of the Image.

  • zoom

    Scale the image.

region

region() -> tuple[int, int, int, int]

Return the decision region of the Image.

zoom

zoom(ratios: tuple[float, float], *, zoom_position: bool = True, zoom_size: bool = True) -> None

Scale the image.

  • ratios: ratios of zooming
  • zoom_position: whether or not to zoom the location of the image
  • zoom_size: whether or not to zoom the size of the image

Style

Style(widget: Widget, *, auto_update: bool | None = None)

The styles of a Widget.

  • widget: parent widget
  • auto_update: whether the theme manager update it automatically

方法:

  • get_disabled_style

    Get the style data of disabled state.

  • init

    Initialize some style of an element.

  • get

    Return the style of the widget.

  • reset

    Reset the style of the widget and update.

  • set

    Set the style of the widget.

get_disabled_style

get_disabled_style(*, element: Element) -> dict[str, str]

Get the style data of disabled state.

  • element: element that style to be calculated

init

init(key: Element | str | int, *, theme: typing.Literal['light', 'dark'] | None = None) -> None

Initialize some style of an element.

  • name: the key of the element
  • theme: the theme name, None indicates both

get

get(
    *, theme: typing.Literal["light", "dark"] | None = None
) -> dict[str, dict[str, dict[str, str]]]

Return the style of the widget.

  • theme: the theme of the widget, None indicates the current theme

reset

reset(*, theme: typing.Literal['light', 'dark'] | None = None) -> None

Reset the style of the widget and update.

  • theme: the theme to be reset, None indicates both

set

set() -> None

Set the style of the widget.

Feature

Feature(widget: Widget)

The features of a Widget.

  • widget: parent widget

方法:

get_method

get_method(name: str) -> collections.abc.Callable

Return method by name.

  • name: name of the method

Widget

Widget(
    master: containers.Canvas | Widget,
    position: tuple[int, int] = (0, 0),
    size: tuple[int, int] | None = None,
    *,
    anchor: typing.Literal["n", "s", "w", "e", "nw", "ne", "sw", "se", "center"] = "nw",
    capture_events: bool | None = None,
    gradient_animation: bool | None = None,
    auto_update: bool | None = None,
    style: type[Style] | None = None
)

Base Widget Class.

Widget = Element + Style + Feature

  • master: parent canvas
  • position: position of the widget
  • size: size of the widget
  • anchor: layout anchor of the widget
  • capture_events: wether detect another widget under the widget
  • gradient_animation: wether enable animation
  • auto_update: whether the theme manager update it automatically
  • style: style of the widget

方法:

  • register_elements

    Register elements to the widget.

  • deregister_elements

    Deregister a element from the widget.

  • update

    Update the widget.

  • bind_on_update

    Bind an extra function to the widget on update.

  • unbind_on_update

    Unbind an extra function to the widget on update.

  • bind

    Bind to this widget at event sequence a call to function command.

  • unbind

    Unbind for this widget the event sequence.

  • generate_event

    Generate an event sequence. Additional keyword arguments specify

  • disable

    Disable the widget.

  • forget

    Let all elements of the widget to forget.

  • move

    Move the widget.

  • moveto

    Move the Widget to a certain position.

  • destroy

    Destroy the widget.

  • detect

    Detect whether the specified coordinates are within the Widget.

  • zoom

    Zoom widget ifself.

属性:

elements property

elements: tuple[Element, ...]

Return all elements of the widget.

nested property

nested: bool

Whether the widget is a nested widget.

offset property

offset: tuple[float, float]

Return the offset of the anchor relative to “nw”.

register_elements

register_elements(*elements: Element) -> None

Register elements to the widget.

  • elements: elements to be registered

deregister_elements

deregister_elements(*elements: Element) -> None

Deregister a element from the widget.

  • elements: elements to be deregistered

update

update(
    state: str | None = None, *, gradient_animation: bool | None = None, nested: bool = True
) -> None

Update the widget.

  • state: state of the widget
  • gradient_animation: whether use gradient animation
  • nested: whether nested

bind_on_update

bind_on_update(command: collections.abc.Callable[[str, bool], typing.Any]) -> None

Bind an extra function to the widget on update.

This extra function has two positional arguments, both of which are arguments to the method update. And this extra function will be called when the widget is updated (whether it’s automatically updated or manually updated).

  • command: the extra function that is bound

unbind_on_update

unbind_on_update(command: collections.abc.Callable[[str, bool], typing.Any]) -> None

Unbind an extra function to the widget on update.

  • command: the extra function that is bound

bind

bind(
    sequence: str,
    command: collections.abc.Callable[[tkinter.Event], typing.Any],
    add: bool | typing.Literal["", "+"] | None = None,
    *,
    auto_detect: bool = True
) -> None

Bind to this widget at event sequence a call to function command.

  • sequence: event name
  • command: callback function
  • add: if True, original callback function will not be overwritten
  • auto_detect: Automatically determine whether to execute binding events based on the method detect

unbind

unbind(sequence: str, command: collections.abc.Callable[[tkinter.Event], typing.Any]) -> None

Unbind for this widget the event sequence.

  • sequence: event name
  • command: callback function

generate_event

generate_event(sequence: str, event: tkinter.Event | None = None, **kwargs) -> None

Generate an event sequence. Additional keyword arguments specify parameter of the event.

  • sequence: event name
  • event: event
  • kwargs: attr of event

disable

disable(value: bool = True) -> None

Disable the widget.

  • value: whether to disable

forget

forget(value: bool = True) -> None

Let all elements of the widget to forget.

  • value: whether to forget the widget

move

move(dx: float, dy: float) -> None

Move the widget.

  • dx: x-coordinate offset
  • dy: y-coordinate offset

moveto

moveto(x: float, y: float) -> None

Move the Widget to a certain position.

  • x: x-coordinate of the target location
  • y: y-coordinate of the target location

destroy

destroy() -> None

Destroy the widget.

detect

detect(x: float, y: float) -> bool

Detect whether the specified coordinates are within the Widget.

  • x: x-coordinate of the location to be detected
  • y: y-coordinate of the location to be detected

zoom

zoom(
    ratios: tuple[float, float] | None = None, *, zoom_position: bool = True, zoom_size: bool = True
) -> None

Zoom widget ifself.

  • ratios: ratios of zooming
  • zoom_position: whether or not to zoom the location of the widget
  • zoom_size: whether or not to zoom the size of the widget