Making a “hovering box” in Tkinter

I have found myself programming GUIs recently using python and Tkinter. I was particularly interested in the providing users with help by creating hover boxes that display a certain message when the mouse is hovering over a certain widget.

I would like to post the code I used to develop this, hopefully someone will find it useful!

from Tkinter import *
import re

class HoverInfo(Menu):
def __init__(self, parent, text, command=None):
   self._com = command
   Menu.__init__(self,parent, tearoff=0)
   if not isinstance(text, str):
      raise TypeError('Trying to initialise a Hover Menu with a non string type: ' + text.__class__.__name__)
   toktext=re.split('\n', text)
   for t in toktext:
      self.add_command(label = t)
      self.master.bind("<Enter>",self.Display )
      self.master.bind("<Leave>",self.Remove )

def __del__(self):

def Display(self,event):
   if not self._displayed:
      self._displayed=True, event.y_root)
   if self._com != None:
      self.master.bind_all("<Return>", self.Click)

def Remove(self, event):
 if self._displayed:
 if self._com != None:

def Click(self, event):


I decided to use Menu widgets because of the post method that allows you to place the floating menu where you like, but I was also interested in allowing for having messages on multiple lines. This works, but is kind of hacky – I am interested in superior solutions!

I also include a simple App that uses this hover box:

from Tkinter import *
from HoverInfo import HoverInfo
class MyApp(Frame):
   def __init__(self, parent=None):
      Frame.__init__(self, parent)
      self.lbl = Label(self, text='testing')

      self.hover = HoverInfo(self, 'while hovering press return \n for an exciting msg', self.HelloWorld)

   def HelloWorld(self):
      print 'Hello World'

app = MyApp()






About jakirkpatrick

I am a researcher in solar energy at the University of Oxford. I am interested in mathematics, programming and trying to understand why things work. I also like the great outdoors and riding my bike.
This entry was posted in Uncategorized. Bookmark the permalink.

1 Response to Making a “hovering box” in Tkinter

  1. Jonny says:

    Thank you very much. This is exactly what I needed. It would’ve taken me forever to figure out how to do this 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s