Skip to content

Commit

Permalink
Updated with a number of features related to text input boxes:
Browse files Browse the repository at this point in the history
Added parameter `show` to mask the original character content;
The new parameter `placeholder` will prompt you when the input box is empty;
The new parameter `align` determines the alignment of the text in the input box, which supports left, right and center methods;
#17: Ctrl+V shortcut key combination is supported to paste text to the cursor.
  • Loading branch information
Xiaokang2022 committed Jul 5, 2024
1 parent 4523386 commit 366b646
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 70 deletions.
25 changes: 25 additions & 0 deletions tkintertools/core/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ def __init__(
self.bind("<ButtonRelease-3>",
lambda event: self._release(event, "right"))

self.bind("<<Copy>>", self._copy)
self.bind("<<Paste>>", self._paste)
self.bind("<<Cut>>", self._cut)

self.bind("<Configure>", lambda _: self._zoom_self())

@property
Expand Down Expand Up @@ -631,3 +635,24 @@ def _input(self, event: tkinter.Event) -> None:
if widget._feature is not None:
if getattr(widget._feature, "_input")(event) and not widget.through:
event.x = math.nan

def _copy(self, event: tkinter.Event) -> None:
"""Events for copy operation"""
for widget in self._widgets[::-1]:
if widget._feature is not None:
if getattr(widget._feature, "_copy")(event) and not widget.through:
pass

def _paste(self, event: tkinter.Event) -> None:
"""Events for paste operation"""
for widget in self._widgets[::-1]:
if widget._feature is not None:
if getattr(widget._feature, "_paste")(event) and not widget.through:
pass

def _cut(self, event: tkinter.Event) -> None:
"""Events for cut operation"""
for widget in self._widgets[::-1]:
if widget._feature is not None:
if getattr(widget._feature, "_cut")(event) and not widget.through:
pass
21 changes: 17 additions & 4 deletions tkintertools/core/virtual.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ def __init__(
text: str = "",
limit: int = math.inf,
show: str | None = None,
placeholder: str = "",
anchor: typing.Literal["n", "e", "w", "s",
"nw", "ne", "sw", "se"] = "center",
justify: typing.Literal["left", "center", "right"] = "left",
Expand Down Expand Up @@ -240,13 +241,15 @@ def __init__(
* `anchor`: anchor of the text
* `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 component
* `animation`: Wether use animation to change color
* `styles`: style dict of component
* `kwargs`: extra parameters for CanvasItem
"""
self.value = text
self.text = text
self.show = show
self.placeholder = placeholder
self.limit = limit
self.anchor = anchor
self.justify = justify
Expand All @@ -255,9 +258,7 @@ def __init__(
size=-abs(fontsize if fontsize else constants.SIZE),
weight=weight, slant=slant,
underline=underline, overstrike=overstrike)
self.left: int = 0
self.right: int = 0
Component.__init__(self, widget, relative_position, size=size,
Component.__init__(self, widget, relative_position, size,
name=name, styles=styles, animation=animation, **kwargs)

def region(self) -> tuple[int, int, int, int]:
Expand Down Expand Up @@ -369,6 +370,18 @@ def _input(self, event: tkinter.Event) -> bool:
"""Event of typing"""
return False

def _copy(self, event: tkinter.Event) -> bool:
"""Event of copy operation"""
return False

def _paste(self, event: tkinter.Event) -> bool:
"""Event of paste operation"""
return False

def _cut(self, event: tkinter.Event) -> bool:
"""Event of cut operation"""
return False


class Widget:
"""
Expand Down
19 changes: 15 additions & 4 deletions tkintertools/standard/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,25 +235,36 @@ def _click_left(self, event: tkinter.Event) -> bool:
True, self.widget._texts[0].items[0])
self.widget._texts[0].cursor_set(
self.widget._texts[0]._text_length())
self.widget.master.itemconfigure(
self.widget._texts[0].items[1], fill="")
else:
if self.widget.state != "normal":
self.widget.update("normal")
if not self.widget._texts[0].get():
self.widget.master.itemconfigure(
self.widget._texts[0].items[1], fill="#787878")
return flag

def _release_left(self, event: tkinter.Event) -> bool:
""""""
return False

def _input(self, event: tkinter.Event) -> bool:
if self.widget.state == "active":
match event.keysym:
case "Right": self.widget._texts[0].move_area(1)
case "Left": self.widget._texts[0].move_area(-1)
case "Right": self.widget._texts[0].move_cursor(1)
case "Left": self.widget._texts[0].move_cursor(-1)
case "BackSpace":
if self.widget._texts[0].value:
if self.widget._texts[0].text:
self.widget._texts[0].pop(1)
case _:
if event.char.isprintable():
if not self.widget._texts[0].limitation():
self.widget._texts[0].append(event.char)
return False

def _paste(self, event: tkinter.Event) -> bool:
if self.widget.state == "active":
if value := self.widget.master.clipboard_get():
self.widget._texts[0].append(value)
return True
return False
Loading

0 comments on commit 366b646

Please sign in to comment.