55 lines
1.7 KiB
Python
55 lines
1.7 KiB
Python
import tkinter
|
|
|
|
|
|
class CreateToolTip(object):
|
|
"""
|
|
create a tooltip for a given widget
|
|
"""
|
|
|
|
def __init__(self, widget, text='widget info'):
|
|
self.waittime = 500 # miliseconds
|
|
self.wraplength = 180 # pixels
|
|
self.widget = widget
|
|
self.text = text
|
|
self.widget.bind("<Enter>", self.enter)
|
|
self.widget.bind("<Leave>", self.leave)
|
|
self.widget.bind("<ButtonPress>", self.leave)
|
|
self.id = None
|
|
self.tw = None
|
|
|
|
def enter(self, event=None):
|
|
self.schedule()
|
|
|
|
def leave(self, event=None):
|
|
self.unschedule()
|
|
self.hidetip()
|
|
|
|
def schedule(self):
|
|
self.unschedule()
|
|
self.id = self.widget.after(self.waittime, self.showtip)
|
|
|
|
def unschedule(self):
|
|
unscheduleid = self.id
|
|
self.id = None
|
|
if unscheduleid:
|
|
self.widget.after_cancel(unscheduleid)
|
|
|
|
def showtip(self, event=None):
|
|
x, y, cx, cy = self.widget.bbox("insert")
|
|
x += self.widget.winfo_rootx() + 25
|
|
y += self.widget.winfo_rooty() + 20
|
|
# creates a toplevel window
|
|
self.tw = tkinter.Toplevel(self.widget)
|
|
# Leaves only the label and removes the app window
|
|
self.tw.wm_overrideredirect(True)
|
|
self.tw.wm_geometry("+%d+%d" % (x, y))
|
|
label = tkinter.Label(self.tw, text=self.text, justify='left',
|
|
background="#ffffff", relief='solid', borderwidth=1,
|
|
wraplength=self.wraplength)
|
|
label.pack(ipadx=1)
|
|
|
|
def hidetip(self):
|
|
tw = self.tw
|
|
self.tw = None
|
|
if tw:
|
|
tw.destroy()
|