cengal.user_interface.gui.tkinter.components.tool_tip.versions.v_0.tool_tip

 1#!/usr/bin/env python
 2# coding=utf-8
 3
 4# Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>
 5# 
 6# Licensed under the Apache License, Version 2.0 (the "License");
 7# you may not use this file except in compliance with the License.
 8# You may obtain a copy of the License at
 9# 
10#     http://www.apache.org/licenses/LICENSE-2.0
11# 
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18
19__all__ = ['ToolTip', 'ToolTipHovered']
20
21
22"""
23Module Docstring
24Docstrings: http://www.python.org/dev/peps/pep-0257/
25"""
26
27__author__ = "ButenkoMS <gtalk@butenkoms.space>"
28__copyright__ = "Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>"
29__credits__ = ["ButenkoMS <gtalk@butenkoms.space>", ]
30__license__ = "Apache License, Version 2.0"
31__version__ = "4.4.1"
32__maintainer__ = "ButenkoMS <gtalk@butenkoms.space>"
33__email__ = "gtalk@butenkoms.space"
34# __status__ = "Prototype"
35__status__ = "Development"
36# __status__ = "Production"
37
38
39import tkinter as tk
40from ttkbootstrap.constants import LEFT as ttkb__LEFT, SOLID as ttkb__SOLID
41
42
43class ToolTip:
44    def __init__(self, widget):
45        self.widget = widget
46        self.tip_window = None
47        self.id = None
48        self.x = self.y = 0
49
50    def show_tip(self, tip_text):
51        "Display text in a tooltip window"
52        self.text = tip_text
53        if self.tip_window or not self.text:
54            return
55        x, y, _, _ = self.widget.bbox("insert")
56        x = x + self.widget.winfo_rootx() + 27
57        y = y + self.widget.winfo_rooty() + 27
58        self.tip_window = tw = tk.Toplevel(self.widget)
59        tw.wm_overrideredirect(True)
60        tw.wm_geometry(f"+{x}+{y}")
61
62        label = tk.Label(tw, text=self.text, justify=ttkb__LEFT,
63                      background="#ffffe0", relief=ttkb__SOLID, borderwidth=1,
64                      font=("tahoma", "8", "normal"))
65        label.pack(ipadx=1)
66
67    def hide_tip(self):
68        if self.tip_window:
69            self.tip_window.destroy()
70            self.tip_window = None
71
72
73class ToolTipHovered(ToolTip):
74    def __init__(self, widget, text: str):
75        super().__init__(widget)
76        self.text: str = text
77        self.widget.bind('<Enter>', lambda e: self.show_tip(self.text))
78        self.widget.bind('<Leave>', lambda e: self.hide_tip())        
class ToolTip:
44class ToolTip:
45    def __init__(self, widget):
46        self.widget = widget
47        self.tip_window = None
48        self.id = None
49        self.x = self.y = 0
50
51    def show_tip(self, tip_text):
52        "Display text in a tooltip window"
53        self.text = tip_text
54        if self.tip_window or not self.text:
55            return
56        x, y, _, _ = self.widget.bbox("insert")
57        x = x + self.widget.winfo_rootx() + 27
58        y = y + self.widget.winfo_rooty() + 27
59        self.tip_window = tw = tk.Toplevel(self.widget)
60        tw.wm_overrideredirect(True)
61        tw.wm_geometry(f"+{x}+{y}")
62
63        label = tk.Label(tw, text=self.text, justify=ttkb__LEFT,
64                      background="#ffffe0", relief=ttkb__SOLID, borderwidth=1,
65                      font=("tahoma", "8", "normal"))
66        label.pack(ipadx=1)
67
68    def hide_tip(self):
69        if self.tip_window:
70            self.tip_window.destroy()
71            self.tip_window = None
ToolTip(widget)
45    def __init__(self, widget):
46        self.widget = widget
47        self.tip_window = None
48        self.id = None
49        self.x = self.y = 0
widget
tip_window
id
def show_tip(self, tip_text):
51    def show_tip(self, tip_text):
52        "Display text in a tooltip window"
53        self.text = tip_text
54        if self.tip_window or not self.text:
55            return
56        x, y, _, _ = self.widget.bbox("insert")
57        x = x + self.widget.winfo_rootx() + 27
58        y = y + self.widget.winfo_rooty() + 27
59        self.tip_window = tw = tk.Toplevel(self.widget)
60        tw.wm_overrideredirect(True)
61        tw.wm_geometry(f"+{x}+{y}")
62
63        label = tk.Label(tw, text=self.text, justify=ttkb__LEFT,
64                      background="#ffffe0", relief=ttkb__SOLID, borderwidth=1,
65                      font=("tahoma", "8", "normal"))
66        label.pack(ipadx=1)

Display text in a tooltip window

def hide_tip(self):
68    def hide_tip(self):
69        if self.tip_window:
70            self.tip_window.destroy()
71            self.tip_window = None
class ToolTipHovered(ToolTip):
74class ToolTipHovered(ToolTip):
75    def __init__(self, widget, text: str):
76        super().__init__(widget)
77        self.text: str = text
78        self.widget.bind('<Enter>', lambda e: self.show_tip(self.text))
79        self.widget.bind('<Leave>', lambda e: self.hide_tip())        
ToolTipHovered(widget, text: str)
75    def __init__(self, widget, text: str):
76        super().__init__(widget)
77        self.text: str = text
78        self.widget.bind('<Enter>', lambda e: self.show_tip(self.text))
79        self.widget.bind('<Leave>', lambda e: self.hide_tip())        
text: str