Skip to content

Commit

Permalink
Merge pull request #77 from gnikit:refactor/default-args
Browse files Browse the repository at this point in the history
Improves default tkinter arguments of ToolTip
  • Loading branch information
gnikit authored Jan 31, 2024
2 parents 238bfc1 + 96ca9bf commit 25bf7fa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
3 changes: 2 additions & 1 deletion examples/color_changer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
import tkinter as tk
from pathlib import Path
from tkinter import ttk

import sv_ttk
Expand All @@ -20,7 +21,7 @@ def __init__(self, parent):

def setup_widgets(self):
self.color = "0,0,0" # Default color to replace (it's black)
img = "tooltip_logo.png" # image name to loads
img = Path(__file__).parent / "tooltip_logo.png" # image name to loads
self.img = Image.open(img).resize((100, 100)) # Load and scale image
self.img_tk = ImageTk.PhotoImage(self.img) # Store to stop garbage collection
self.img_label = ttk.Label(self, image=self.img_tk) # Create label with image
Expand Down
15 changes: 12 additions & 3 deletions tktooltip/tooltip.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class ToolTip(tk.Toplevel):
Creates a ToolTip (pop-up) widget for tkinter
"""

DEFAULT_PARENT_KWARGS = {"bg": "black", "padx": 1, "pady": 1}
DEFAULT_MESSAGE_KWARGS = {"aspect": 1000}

def __init__(
self,
widget: tk.Widget,
Expand All @@ -26,7 +29,7 @@ def __init__(
refresh: float = 1.0,
x_offset: int = +10,
y_offset: int = +10,
parent_kwargs: dict[Any, Any] = {"bg": "black", "padx": 1, "pady": 1},
parent_kwargs: dict | None = None,
**message_kwargs: Any,
):
"""Create a ToolTip. Allows for `**kwargs` to be passed on both
Expand Down Expand Up @@ -59,7 +62,7 @@ def __init__(
self.widget = widget
# ToolTip should have the same parent as the widget unless stated
# otherwise in the `parent_kwargs`
tk.Toplevel.__init__(self, **parent_kwargs)
tk.Toplevel.__init__(self, **(parent_kwargs or self.DEFAULT_PARENT_KWARGS))
self.withdraw() # Hide initially in case there is a delay
# Disable ToolTip's title bar
self.overrideredirect(True)
Expand All @@ -86,7 +89,13 @@ def __init__(
self.status = "outside"
self.last_moved = 0
# use Message widget to host ToolTip
tk.Message(self, textvariable=self.msgVar, aspect=1000, **message_kwargs).grid()
self.message_kwargs: dict = message_kwargs or self.DEFAULT_MESSAGE_KWARGS
self.message_widget = tk.Message(
self,
textvariable=self.msgVar,
**self.message_kwargs,
)
self.message_widget.grid()
# Add bindings to the widget without overriding the existing ones
self.widget.bind("<Enter>", self.on_enter, add="+")
self.widget.bind("<Leave>", self.on_leave, add="+")
Expand Down

0 comments on commit 25bf7fa

Please sign in to comment.