add new class for tooltip

This commit is contained in:
Désiré Werner Menrath 2025-04-21 13:29:36 +02:00
parent 8896f59efd
commit 2a3bf2bbcb
3 changed files with 39 additions and 1 deletions

View File

@ -389,3 +389,41 @@ def if_tip(path):
else:
tip = True
return tip
class Tooltip:
"""
class for Tooltip
imoprt Tooltip
example: Tooltip(label, "Show tooltip on label")
examble: Tooltip(button, "Show tooltip on button")
info: label and button is parrent.
"""
def __init__(self, widget, text):
self.widget = widget
self.text = text
self.tooltip_window = None
self.widget.bind("<Enter>", self.show_tooltip)
self.widget.bind("<Leave>", self.hide_tooltip)
def show_tooltip(self, event=None):
if self.tooltip_window or not self.text:
return
x, y, _, _ = self.widget.bbox("insert")
x += self.widget.winfo_rootx() + 25
y += self.widget.winfo_rooty() + 20
self.tooltip_window = tw = tk.Toplevel(self.widget)
tw.wm_overrideredirect(True)
tw.wm_geometry(f"+{x}+{y}")
label = tk.Label(tw, text=self.text, relief="solid", borderwidth=1, padx=5, pady=5)
label.grid()
def hide_tooltip(self, event=None):
if self.tooltip_window:
self.tooltip_window.destroy()
self.tooltip_window = None

View File

@ -14,7 +14,7 @@ import tkinter as tk
from pathlib import Path
from subprocess import check_call
from tkinter import filedialog, ttk, TclError
from cls_mth_fc import Tunnel, Create, msg_window, if_tip, GiteaUpdate, uos
from cls_mth_fc import Tooltip, Tunnel, Create, msg_window, if_tip, GiteaUpdate, uos
uos()
Create.dir_and_files()