Skip to content

Commit

Permalink
Create tooltips module
Browse files Browse the repository at this point in the history
  • Loading branch information
RedFantom committed Nov 26, 2019
1 parent f3fb2ff commit 2572157
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
13 changes: 13 additions & 0 deletions examples/example_tooltips.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
Author: RedFantom
License: GNU GPLv3
Source: The ttkwidgets repository
"""
import tkinter as tk
from tkinter import ttk
from ttkwidgets import tooltips # Import once, use everywhere


window = tk.Tk()
ttk.Button(window, text="Destroy", command=window.destroy, tooltip="This button destroys the window.").pack()
window.mainloop()
87 changes: 87 additions & 0 deletions ttkwidgets/tooltips.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""
Author: RedFantom
License: GNU GPLv3
Source: The ttkwidgets repository
By importing this file at some point in a program, the
ttk.Widget parent classes get dynamically mixed in with class that
adds the functionality of adding a tooltip (Balloon) to any widget.
Tooltips are only added when a string to show in them is explicitly
given. Options for a Balloon may be given as a dictionary of keyword
arguments upon widget initialization.
The default options for the Balloon widget for the program may also be
changed by using the update_defaults function.
# TODO: Convert current implementation to a mixin
# TODO: Apply mixin to tkWidget also
"""
try:
import Tkinter as tk
import ttk
except ImportError:
import tkinter as tk
from tkinter import ttk
from ttkwidgets.frames import Balloon


DEFAULTS = {}


def update_defaults(defaults):
# type: (dict) -> None
global DEFAULTS
DEFAULTS.update(defaults)


class ToolTippableWidget(ttk.Widget):
def __init__(self, *args):
master, widget_type, kwargs = args
self._tooltip = kwargs.pop("tooltip", None)
self._tooltip_options = DEFAULTS.copy()
self._tooltip_options.update(kwargs.pop("tooltip_options", {}))
ttk.Widget._init__original(self, master, widget_type, kwargs)
self.__widget = None
self._update_tooltip()

def configure(self, *args, **kwargs):
self._tooltip = kwargs.pop("tooltip", None)
self._tooltip_options.update(kwargs.pop("tooltip_options", {}))
self._update_tooltip()
return ttk.Widget._configure_original(self, *args, **kwargs)

def cget(self, key):
if key == "tooltip":
return self._tooltip
elif key == "tooltip_options":
return self._tooltip_options
return ttk.Widget._cget_original(self, key)

def config(self, *args, **kwargs):
return self.configure(*args, **kwargs)

def _update_tooltip(self):
self._tooltip_options["text"] = self._tooltip
if self._tooltip is None and self.__widget is not None:
self.__widget.destroy()
elif self._tooltip is not None and self.__widget is not None:
self.__widget.configure(**self._tooltip_options)
elif not isinstance(self, Balloon): # Balloons may not have Balloons -> recursion
self.__widget = Balloon(self, **self._tooltip_options)


if ttk.Widget.__init__ is not ToolTippableWidget.__init__:

# Save the original functions
ttk.Widget._init__original = ttk.Widget.__init__
ttk.Widget._configure_original = ttk.Widget.configure
ttk.Widget._config_original = ttk.Widget.config
ttk.Widget._cget_original = ttk.Widget.cget

# Apply the modified functions
ttk.Widget.__init__ = ToolTippableWidget.__init__
ttk.Widget.configure = ToolTippableWidget.configure
ttk.Widget.config = ToolTippableWidget.config
ttk.Widget.cget = ToolTippableWidget.cget
ttk.Widget._update_tooltip = ToolTippableWidget._update_tooltip

0 comments on commit 2572157

Please sign in to comment.