Create a tooltip for a given widget (inspired by https://stackoverflow.com/a/36221216) This is an INTERNALLY USED only class. Users should not refer to this class at all.
| 839 | # ------------------------------------------------------------------------- # |
| 840 | |
| 841 | class ToolTip: |
| 842 | """ |
| 843 | Create a tooltip for a given widget |
| 844 | (inspired by https://stackoverflow.com/a/36221216) |
| 845 | This is an INTERNALLY USED only class. Users should not refer to this class at all. |
| 846 | """ |
| 847 | |
| 848 | def __init__(self, widget, text, timeout=DEFAULT_TOOLTIP_TIME): |
| 849 | """ |
| 850 | :param widget: The tkinter widget |
| 851 | :type widget: widget type varies |
| 852 | :param text: text for the tooltip. It can inslude \n. If None tip won't be shown |
| 853 | :type text: str | None |
| 854 | :param timeout: Time in milliseconds that mouse must remain still before tip is shown |
| 855 | :type timeout: (int) |
| 856 | """ |
| 857 | self.widget = widget |
| 858 | self.text = text # Set to None and tooltip will be not shown |
| 859 | self.timeout = timeout |
| 860 | # self.wraplength = wraplength if wraplength else widget.winfo_screenwidth() // 2 |
| 861 | self.tipwindow = None |
| 862 | self.id = None |
| 863 | self.x = self.y = 0 |
| 864 | self.widget.bind("<Enter>", self.enter) |
| 865 | self.widget.bind("<Leave>", self.leave) |
| 866 | self.widget.bind("<ButtonPress>", self.leave) |
| 867 | |
| 868 | def enter(self, event=None): |
| 869 | """ |
| 870 | Called by tkinter when mouse enters a widget |
| 871 | :param event: from tkinter. Has x,y coordinates of mouse |
| 872 | :type event: |
| 873 | |
| 874 | """ |
| 875 | if self.text is None: # if tip is diabled |
| 876 | return |
| 877 | self.x = event.x |
| 878 | self.y = event.y |
| 879 | self.schedule() |
| 880 | |
| 881 | def leave(self, event=None): |
| 882 | """ |
| 883 | Called by tktiner when mouse exits a widget |
| 884 | :param event: from tkinter. Event info that's not used by function. |
| 885 | :type event: |
| 886 | |
| 887 | """ |
| 888 | self.unschedule() |
| 889 | self.hidetip() |
| 890 | |
| 891 | def schedule(self): |
| 892 | """ |
| 893 | Schedule a timer to time how long mouse is hovering |
| 894 | """ |
| 895 | self.unschedule() |
| 896 | self.id = self.widget.after(self.timeout, self.showtip) |
| 897 | |
| 898 | def unschedule(self): |
no outgoing calls
no test coverage detected