Python3 uses tkinter for interface mouse prompt (tooltip)

Source: Internet
Author: User

From tkinter import *
From time import time, localtime, strftime

Class tooltip (toplevel ):
"""
Provides a tooltip widget for tkinter.
To apply a tooltip to any tkinter widget, simply pass the widget to
Tooltip Constructor
"""
Def _ init _ (self, wdgt, MSG = none, msgfunc = none, delay = 1, follow = true ):
"""
Initialize the tooltip

Arguments:
Wdgt: the widget this tooltip is assigned
MSG: a static string message assigned to the tooltip
Msgfunc: a function that retrieves a string to use as the tooltip text
Delay: the delay in seconds before the tooltip appears (may be float)
Follow: if true, The tooltip follows motion, otherwise hides
"""
Self. wdgt = wdgt
Self. Parent = self. wdgt. master # the parent of the tooltip is the parent of the tooltips widget
Toplevel. _ init _ (self, self. Parent, BG = 'black', padx = 1, pady = 1) # initalise the toplevel
Self. Withdraw () # Hide initially
Self. overrideredirect (true) # The tooltip toplevel shocould have no frame or title bar

Self. msgvar = stringvar () # The msgvar will contain the text displayed by the tooltip
If MSG = none:
Self. msgvar. Set ('no message provided ')
Else:
Self. msgvar. Set (MSG)
Self. msgfunc = msgfunc
Self. Delay = Delay
Self. Follow = follow
Self. Visible = 0
Self. lastmotion = 0
Message (self, textvariable = self. msgvar, BG = '# ffffdd ',
Aspect = 1000). Grid () # The test of the tooltip is displayed in a message widget
Self. wdgt. BIND ('<enter>', self. spawn, '+') # Add bindings to the widget. This will not override bindings that the widget already has
Self. wdgt. BIND ('<leave>', self. Hide, '+ ')
Self. wdgt. BIND ('<motion>', self. Move, '+ ')

Def spawn (self, event = none ):
"""
Spawn the tooltip. This simply makes the tooltip eligible for display.
Usually this is caused by entering the widget

Arguments:
Event: the event that called this funciton
"""
Self. Visible = 1
Self. After (INT (self. Delay * 1000), self. Show) # The after function takes a time argument in miliseconds

Def show (Self ):
"""
Displays the tooltip if the time delay has been long enough
"""
If self. Visible = 1 and time ()-self. lastmotion> self. Delay:
Self. Visible = 2
If self. Visible = 2:
Self. deiconify ()

Def move (self, event ):
"""
Processes motion within the widget.

Arguments:
Event: the event that called this function
"""
Self. lastmotion = Time ()
If self. Follow = false: # If the follow flag is not set, motion within the widget will make the tooltip dissapear
Self. Withdraw ()
Self. Visible = 1
Self. Geometry ('+ % I + % I' % (event. x_root + 10, event. y_root + 10) # offset the tooltip 10x10 pixes southwest of the pointer
Try:
Self. msgvar. Set (self. msgfunc () # try to call the message function. will not change the message if the message function is none or the message function fails
Except t:
Pass
Self. after( int (self. Delay * 1000), self. Show)

Def hide (self, event = none ):
"""
Hides the tooltip. Usually this is caused by leaving the widget

Arguments:
Event: the event that called this function
"""
Self. Visible = 0
Self. Withdraw ()

Def xrange2d (n, m ):
"""
Returns a generator of values in a 2D range

Arguments:
N: The number of rows in the 2D range
M: The number of columns in the 2D range
Returns:
A generator of values in a 2D range
"""
Return (I, j) for I in xrange (n) for J in xrange (m ))

Def range2d (n, m ):
"""
Returns a list of values in a 2D range

Arguments:
N: The number of rows in the 2D range
M: The number of columns in the 2D range
Returns:
A list of values in a 2D range
"""
Return [(I, j) for I in range (n) for J in range (m)]

Def print_time ():
"""
Prints the current time in the following format:
Hh: mm: ss.00
"""
T = time ()
Timestring = 'time ='
Timestring + = strftime ('% H: % m:', localtime (t ))
Timestring + = '%. 2f' % (T % 60 ,)
Return timestring

Def main ():
Root = TK ()
Btnlist = []
For (I, j) in range2d (6, 4 ):
TEXT = 'delay = % I \ n' % I
Delay = I
If j> = 2:
Follow = true
Text + = '+ follow \ N'
Else:
Follow = false
Text + = '-Follow \ N'
If J % 2 = 0:
MSG = none
Msgfunc = print_time
Text + = 'message function'
Else:
MSG = 'button at % s' % STR (I, j ))
Msgfunc = none
Text + = 'static message'
Btnlist. append (button (root, text = text ))
Tooltip (btnlist [-1], MSG = MSG, msgfunc = msgfunc, follow = follow, delay = delay)
Btnlist [-1]. Grid (ROW = I, column = J, sticky = N + S + E + W)
Root. mainloop ()

If _ name _ = '_ main __':
Main ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.