Book Image

Python GUI Programming Cookbook

By : Burkhard Meier
Book Image

Python GUI Programming Cookbook

By: Burkhard Meier

Overview of this book

Table of Contents (18 chapters)
Python GUI Programming Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating tooltips using Python


This recipe will show us how to create ToolTips. When the user hovers the mouse over a widget, additional information will be available in the form of a ToolTip.

We will code this additional information into our GUI.

Getting ready

We are adding more useful functionality to our GUI. Surprisingly, adding a ToolTip to our controls should be simple, but it is not as simple as we wish it to be.

In order to achieve this desired functionality, we will place our ToolTip code into its own OOP class.

How to do it...

Add this class just below the import statements:

class ToolTip(object):
    def __init__(self, widget):
        self.widget = widget
        self.tipwindow = None
        self.id = None
        self.x = self.y = 0

    def showtip(self, text):
        "Display text in tooltip window"
        self.text = text
        if self.tipwindow or not self.text:
            return
        x, y, _cx, cy = self.widget.bbox("insert")
        x = x + self.widget.winfo_rootx()...