2019-09-27 23:28:51 +01:00
|
|
|
import tkinter as tk
|
2019-11-12 21:01:58 +00:00
|
|
|
from tkinter import ttk
|
2020-06-20 06:08:24 +01:00
|
|
|
from typing import Optional
|
2019-09-27 23:28:51 +01:00
|
|
|
|
2019-12-19 17:30:21 +00:00
|
|
|
from core.gui.themes import Styles
|
2019-11-13 18:45:43 +00:00
|
|
|
|
2019-09-27 23:28:51 +01:00
|
|
|
|
2019-11-12 21:01:58 +00:00
|
|
|
class Tooltip(object):
|
2019-09-27 23:28:51 +01:00
|
|
|
"""
|
|
|
|
Create tool tip for a given widget
|
|
|
|
"""
|
|
|
|
|
2020-06-20 06:08:24 +01:00
|
|
|
def __init__(self, widget: tk.BaseWidget, text: str = "widget info") -> None:
|
|
|
|
self.widget: tk.BaseWidget = widget
|
|
|
|
self.text: str = text
|
2019-11-12 21:01:58 +00:00
|
|
|
self.widget.bind("<Enter>", self.on_enter)
|
|
|
|
self.widget.bind("<Leave>", self.on_leave)
|
2020-06-20 06:08:24 +01:00
|
|
|
self.waittime: int = 400
|
|
|
|
self.id: Optional[str] = None
|
|
|
|
self.tw: Optional[tk.Toplevel] = None
|
2019-09-27 23:28:51 +01:00
|
|
|
|
2020-06-20 06:08:24 +01:00
|
|
|
def on_enter(self, event: tk.Event = None) -> None:
|
2019-11-12 21:01:58 +00:00
|
|
|
self.schedule()
|
|
|
|
|
2020-06-20 06:08:24 +01:00
|
|
|
def on_leave(self, event: tk.Event = None) -> None:
|
2019-11-12 21:01:58 +00:00
|
|
|
self.unschedule()
|
|
|
|
self.close(event)
|
|
|
|
|
|
|
|
def schedule(self):
|
|
|
|
self.unschedule()
|
|
|
|
self.id = self.widget.after(self.waittime, self.enter)
|
|
|
|
|
|
|
|
def unschedule(self):
|
|
|
|
id_ = self.id
|
|
|
|
self.id = None
|
|
|
|
if id_:
|
|
|
|
self.widget.after_cancel(id_)
|
|
|
|
|
2020-01-14 19:06:52 +00:00
|
|
|
def enter(self, event: tk.Event = None):
|
2019-09-27 23:28:51 +01:00
|
|
|
x, y, cx, cy = self.widget.bbox("insert")
|
|
|
|
x += self.widget.winfo_rootx()
|
|
|
|
y += self.widget.winfo_rooty() + 32
|
|
|
|
self.tw = tk.Toplevel(self.widget)
|
|
|
|
self.tw.wm_overrideredirect(True)
|
|
|
|
self.tw.wm_geometry("+%d+%d" % (x, y))
|
2019-11-13 18:45:43 +00:00
|
|
|
self.tw.rowconfigure(0, weight=1)
|
|
|
|
self.tw.columnconfigure(0, weight=1)
|
|
|
|
frame = ttk.Frame(self.tw, style=Styles.tooltip_frame, padding=3)
|
|
|
|
frame.grid(sticky="nsew")
|
|
|
|
label = ttk.Label(frame, text=self.text, style=Styles.tooltip)
|
|
|
|
label.grid()
|
2019-09-27 23:28:51 +01:00
|
|
|
|
2020-01-14 19:06:52 +00:00
|
|
|
def close(self, event: tk.Event = None):
|
2019-09-27 23:28:51 +01:00
|
|
|
if self.tw:
|
|
|
|
self.tw.destroy()
|