Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new widget and a new feature #66

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions maliang/standard/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,30 @@
return flag


class IconOnlyFeature(ButtonFeature):
"""Feature of Icon Only Button"""
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docstring 里面的类名不要拆开来写。


def _motion(self, event: tkinter.Event) -> bool:
if flag := self.widget.images[0].detect(event.x, event.y):
cursor = utility.fix_cursor(

Check warning on line 106 in maliang/standard/features.py

View check run for this annotation

Codecov / codecov/patch

maliang/standard/features.py#L105-L106

Added lines #L105 - L106 were not covered by tests
"disabled" if self.widget.state == "disabled" else "hand2")
self.widget.master.trigger_config.update(cursor=cursor)
if self.widget.state == "normal":
self.widget.update("hover")

Check warning on line 110 in maliang/standard/features.py

View check run for this annotation

Codecov / codecov/patch

maliang/standard/features.py#L108-L110

Added lines #L108 - L110 were not covered by tests
else:
if self.widget.state != "normal":
self.widget.update("normal")
return flag

Check warning on line 114 in maliang/standard/features.py

View check run for this annotation

Codecov / codecov/patch

maliang/standard/features.py#L112-L114

Added lines #L112 - L114 were not covered by tests

def _button_release_1(self, event: tkinter.Event) -> bool:
if flag := self.widget.images[0].detect(event.x, event.y):
if self.widget.state == "active":
self.widget.update("hover")
if self.command is not None:
self.command(*self._args)
return flag

Check warning on line 122 in maliang/standard/features.py

View check run for this annotation

Codecov / codecov/patch

maliang/standard/features.py#L117-L122

Added lines #L117 - L122 were not covered by tests


class Underline(ButtonFeature):
"""Feature of underline"""

Expand Down
51 changes: 51 additions & 0 deletions maliang/standard/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,57 @@
return self.texts[0].set(text)


class IconOnlyButton(IconButton):
"""A button with nothing but an icon"""

def __init__(
self,
master: containers.Canvas | virtual.Widget,
position: tuple[int, int],
size: tuple[int, int] | None = None,
*,
command: collections.abc.Callable | None = None,
image: enhanced.PhotoImage | None = None,
borderless=True,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此处缺少类型提示,请完善。

anchor: typing.Literal["n", "e", "w", "s", "nw", "ne", "sw", "se", "center"] = "nw",
capture_events: bool | None = None,
gradient_animation: bool | None = None,
auto_update: bool | None = None,
style: type[virtual.Style] | None = None,
) -> None:
"""
* `master`: parent canvas
* `position`: position of the widget
* `size`: size of the widget
* `command`: a function that is triggered when the button is pressed
* `image`: image of the widget
* `anchor`: anchor of the widget
* `capture_events`: wether detect another widget under the widget
* `gradient_animation`: wether enable gradient_animation
* `auto_update`: whether the theme manager update it automatically
* `style`: style of the widget
"""
if size is None:
size = image.width(), image.height()
virtual.Widget.__init__(

Check warning on line 1005 in maliang/standard/widgets.py

View check run for this annotation

Codecov / codecov/patch

maliang/standard/widgets.py#L1003-L1005

Added lines #L1003 - L1005 were not covered by tests
self, master, position, size, anchor=anchor,
capture_events=capture_events, gradient_animation=gradient_animation,
auto_update=auto_update, style=style)
if style is None:
self.style = styles.TextStyle(self) if borderless else styles.IconButtonStyle(self)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此处应该不需要 Style 了,因为只有图片的控件或许不会涉及到样式。

在其超类 virtual.Widget 中会提供默认的 Style 实例以供用户扩展功能。

if image is not None:
images.StillImage(self, ((size[1]-size[0]) / 2, 0), image=image)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此处以及上面的参数部分应该改一改,参数 image 不允许为 None 了,如果没有图片,那这个控件的意义在哪里呢?所以应该强制指定,参数 image 必须为对应的实例。

self.feature = features.IconOnlyFeature(self, command=command)

Check warning on line 1013 in maliang/standard/widgets.py

View check run for this annotation

Codecov / codecov/patch

maliang/standard/widgets.py#L1009-L1013

Added lines #L1009 - L1013 were not covered by tests

def get(self) -> Exception:
"""This class has nothing to get"""
raise AttributeError()

Check warning on line 1017 in maliang/standard/widgets.py

View check run for this annotation

Codecov / codecov/patch

maliang/standard/widgets.py#L1017

Added line #L1017 was not covered by tests

def set(self, thing: None) -> Exception:
"""This class has nothing to set"""
raise AttributeError()

Check warning on line 1021 in maliang/standard/widgets.py

View check run for this annotation

Codecov / codecov/patch

maliang/standard/widgets.py#L1021

Added line #L1021 was not covered by tests


class Slider(virtual.Widget):
"""A slider for visually resizing values"""

Expand Down
Loading