Adding a tooltip and finalizing our player

In this iteration, we add tooltip also named the Balloon widget to all the buttons in our player.

A tooltip is a small popup, which shows up when you hover your mouse over the Bound widget (buttons in our case). A typical tooltip on our application would look as shown in the following screenshot:

Adding a tooltip and finalizing our player

Prepare for Lift Off

Although core Tkinter has many useful widgets, it is far from complete. For us, the tooltip or Balloon widget is not provided as a core Tkinter widget. We, therefore, look for these widgets in what are named Tkinter extensions.

These extensions are nothing but modified Tkinter widgets to act and behave with new functionalities not offered by Tkinter.

There are literally hundreds of Tkinter extensions. In fact, we could write our own Tkinter extensions. However, some of the popular Tkinter extensions are as follows:

PMW list of extensions

Talking about PMW, here is a quick list of widget extensions from the package:

Widgets

  • ButtonBox
  • ComboBox
  • Counter
  • EntryField
  • Group
  • HistoryText
  • LabeledWidget
  • MainMenuBar
  • MenuBar
  • MessageBar
  • NoteBook
  • OptionMenu
  • PanedWidget
  • RadioSelect
  • ScrolledCanvas
  • ScrolledField
  • ScrolledFrame
  • ScrolledListBox
  • ScrolledText
  • TimeCounter

Dialogs

  • AboutDialog
  • ComboBoxDialog
  • CounterDialog
  • Dialog
  • MessageDialog
  • PromptDialog
  • SelectionDialog
  • TextDialog

Miscellaneous

  • Balloon
  • Blt(used for graph generation)
  • Color Module functions

Tip

PMW offers a large list of extended widgets. For a demonstration of all these widgets, browse into the PMW package that you installed earlier, and look for a directory named demo. Within demo, look for a file, all.py, which demonstrates all PMW extensions with sample working code.

Engage Thrusters

Step 1 – importing PMW

PMW provides the Balloon widget implementation, but it is not part of standard Tkinter library. We need to add it. To add PMW, refer to our discussion under the Mission Checklist section. Once added, you need to import PMW into your namespace, as follows (see Code 5.07 main-gui.py):

import Pmw

Step 2 – instantiating the Balloon widget

We then instantiate the Balloon widget within the mainloop from our __init__ method as follows:

   self.balloon = Pmw.Balloon(self.root)

Step 3 – adding Balloon tooltips to all buttons in our player

Finally, we bind the Balloon widget to each of the button widgets in our player. We will not reproduce the code for each button. However, the format is as follows:

balloon.bind(name of widget, 'Description for the balloon')

So our Add File button would have a balloon binding as follows:

self.balloon.bind(add_filebtn, 'Add New File')

We add similar code for each button in 5.07 main-gui.py.

Before we end this iteration, let us add a title to our player and add a title bar icon as well, as follows:

self.root.title('Media Player')
self.root.iconbitmap('../icons/mp.ico')

Objective Complete – Mini Debriefing

This completes the iteration. We added Balloon tooltips to our player buttons using PMW Tkinter extension.

Most importantly, we got to know about Tkinter extensions and when to use them.

Tip

When you come across a widget implementation need that is not available as a core widget, try looking for implementations of it in PMW or Tix. If you don't find one that suits your need, search the Internet for some other Tkinter extension.

If you still don't find your desired implementation, try out WCK, which lets you implement all types of custom widgets. However, note that WCK is not under active development for long.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.156.251