Python-Ref > GUI programming with PyGTK > Widgets > Tooltip
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Tooltip

How to display simple text hints above widget when holding mouse over them.
From version GTK+ 2.12 a new way to set tooltips was introduced. Every widget that inherits from gtk.Widget has the methods "set_tooltip_text" and "set_tooltip_markup" available that take care of tooltip creation.
The following example adds tooltips to the first example from Buttons.
Expand/Shrink
  1   # ensure that PyGTK 2.0 is loaded - not an older version
  2   import pygtk
  3   pygtk.require('2.0')
  4   # import the GTK module
  5   import gtk
  6   
  7   class MyGUI:
  8   
  9     def __init__( self, title):
 10       self.window = gtk.Window()
 11       self.title = title
 12       self.window.set_title( title)
 13       self.window.connect( "destroy", self.destroy)
 14       self.create_interior()
 15       self.window.show_all()
 16   
 17     def create_interior( self):
 18       self.mainbox = gtk.VBox()
 19       self.window.add( self.mainbox)
 20       # box for text
 21       self.text_box = gtk.VBox()
 22       self.mainbox.pack_start( self.text_box)
 23       self.label = gtk.Label( "Hello world")
 24       self.text_box.pack_start( self.label, padding=10)
 25       self.label.show()
 26       self.label.set_tooltip_text( "This is a label, what do you need a tip for?!")
 27       self.text_box.show()
 28       # box for buttons
 29       self.button_box = gtk.HBox()
 30       self.mainbox.pack_end( self.button_box)
 31       # first button
 32       button = gtk.Button( "Press me")
 33       button.connect( "clicked", self.button1_clicked)
 34       self.button_box.pack_start( button)
 35       button.show()
 36       button.set_tooltip_text( "This is the good button.")
 37       # second button
 38       button = gtk.Button( "Big red button")
 39       button.connect( "clicked", self.red_button_clicked)
 40       self.button_box.pack_start( button)
 41       button.show()
 42       button.set_tooltip_markup( "Do <b>not</b> press this button!")
 43       # show the box
 44       self.button_box.show()
 45       self.mainbox.show()
 46   
 47     def main( self):
 48       gtk.main()
 49   
 50     def destroy( self, w):
 51       gtk.main_quit()
 52   
 53     def button1_clicked( self, w):
 54       self.label.set_label( "You clicked the right button")
 55   
 56     def red_button_clicked( self, w):
 57       self.label.set_label( "Please don't press this button again")
 58   
 59   
 60   if __name__ == "__main__":
 61     m = MyGUI( "Tooltips")
 62     m.main()
Screenshot:
Program screenshot gtk-tooltip1-1.pngProgram screenshot gtk-tooltip1-1a.png
Doba běhu: 3181.4 ms