@@ -631,7 +631,7 @@ def __init__(self, canvas, window=None, *, pack_toolbar=True):
631
631
command = getattr (self , callback ),
632
632
)
633
633
if tooltip_text is not None :
634
- ToolTip . createToolTip (button , tooltip_text )
634
+ add_tooltip (button , tooltip_text )
635
635
636
636
self ._label_font = tkinter .font .Font (root = window , size = 10 )
637
637
@@ -892,62 +892,44 @@ def set_history_buttons(self):
892
892
state_map = {True : tk .NORMAL , False : tk .DISABLED }
893
893
can_back = self ._nav_stack ._pos > 0
894
894
can_forward = self ._nav_stack ._pos < len (self ._nav_stack ) - 1
895
-
896
895
if "Back" in self ._buttons :
897
896
self ._buttons ['Back' ]['state' ] = state_map [can_back ]
898
-
899
897
if "Forward" in self ._buttons :
900
898
self ._buttons ['Forward' ]['state' ] = state_map [can_forward ]
901
899
902
900
903
- class ToolTip :
904
- """
905
- Tooltip recipe from
906
- http://www.voidspace.org.uk/python/weblog/arch_d7_2006_07_01.shtml#e387
907
- """
908
- @staticmethod
909
- def createToolTip (widget , text ):
910
- toolTip = ToolTip (widget )
911
- def enter (event ):
912
- toolTip .showtip (text )
913
- def leave (event ):
914
- toolTip .hidetip ()
915
- widget .bind ('<Enter>' , enter )
916
- widget .bind ('<Leave>' , leave )
917
-
918
- def __init__ (self , widget ):
919
- self .widget = widget
920
- self .tipwindow = None
921
- self .id = None
922
- self .x = self .y = 0
923
-
924
- def showtip (self , text ):
901
+ def add_tooltip (widget , text ):
902
+ tipwindow = None
903
+
904
+ def showtip (event ):
925
905
"""Display text in tooltip window."""
926
- self . text = text
927
- if self . tipwindow or not self . text :
906
+ nonlocal tipwindow
907
+ if tipwindow or not text :
928
908
return
929
- x , y , _ , _ = self .widget .bbox ("insert" )
930
- x = x + self .widget .winfo_rootx () + self .widget .winfo_width ()
931
- y = y + self .widget .winfo_rooty ()
932
- self .tipwindow = tw = tk .Toplevel (self .widget )
933
- tw .wm_overrideredirect (1 )
934
- tw .wm_geometry ("+%d+%d" % (x , y ))
935
- try :
936
- # For Mac OS
937
- tw .tk .call ("::tk::unsupported::MacWindowStyle" ,
938
- "style" , tw ._w ,
939
- "help" , "noActivates" )
909
+ x , y , _ , _ = widget .bbox ("insert" )
910
+ x = x + widget .winfo_rootx () + widget .winfo_width ()
911
+ y = y + widget .winfo_rooty ()
912
+ tipwindow = tk .Toplevel (widget )
913
+ tipwindow .overrideredirect (1 )
914
+ tipwindow .geometry (f"+{ x } +{ y } " )
915
+ try : # For Mac OS
916
+ tipwindow .tk .call ("::tk::unsupported::MacWindowStyle" ,
917
+ "style" , tipwindow ._w ,
918
+ "help" , "noActivates" )
940
919
except tk .TclError :
941
920
pass
942
- label = tk .Label (tw , text = self . text , justify = tk .LEFT ,
921
+ label = tk .Label (tipwindow , text = text , justify = tk .LEFT ,
943
922
relief = tk .SOLID , borderwidth = 1 )
944
923
label .pack (ipadx = 1 )
945
924
946
- def hidetip (self ):
947
- tw = self .tipwindow
948
- self .tipwindow = None
949
- if tw :
950
- tw .destroy ()
925
+ def hidetip (event ):
926
+ nonlocal tipwindow
927
+ if tipwindow :
928
+ tipwindow .destroy ()
929
+ tipwindow = None
930
+
931
+ widget .bind ("<Enter>" , showtip )
932
+ widget .bind ("<Leave>" , hidetip )
951
933
952
934
953
935
@backend_tools ._register_tool_class (FigureCanvasTk )
@@ -1002,7 +984,7 @@ def add_toolitem(
1002
984
lambda : self ._button_click (name ))
1003
985
button .pack_configure (before = before )
1004
986
if description is not None :
1005
- ToolTip . createToolTip (button , description )
987
+ add_tooltip (button , description )
1006
988
self ._toolitems .setdefault (name , [])
1007
989
self ._toolitems [name ].append (button )
1008
990
0 commit comments