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