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._displayed=False
      self.master.bind("<Enter>",self.Display )
      self.master.bind("<Leave>",self.Remove )

def __del__(self):
   self.master.unbind("<Enter>")
   self.master.unbind("<Leave>")

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

def Remove(self, event):
 if self._displayed:
   self._displayed=False
   self.unpost()
 if self._com != None:
   self.unbind_all("<Return>")

def Click(self, event):
   self._com()

 

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.grid()
      self.lbl = Label(self, text='testing')
      self.lbl.grid()

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

   def HelloWorld(self):
      print 'Hello World'

app = MyApp()
app.master.title('test')
app.mainloop()

 

 

 

 

 

Advertisements

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.

One 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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s