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

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

Buttons

How to use buttons.
Expand/Shrink
Zdroj: (gtk4-1.py)
  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.text_box.show()
 27       # box for buttons
 28       self.button_box = gtk.HBox()
 29       self.mainbox.pack_end( self.button_box)
 30       # first button
 31       button = gtk.Button( "Press me")
 32       button.connect( "clicked", self.button1_clicked)
 33       self.button_box.pack_start( button)
 34       button.show()
 35       # second button
 36       button = gtk.Button( "Big red button")
 37       button.connect( "clicked", self.red_button_clicked)
 38       self.button_box.pack_start( button)
 39       button.show()
 40       # show the box
 41       self.button_box.show()
 42       self.mainbox.show()
 43   
 44     def main( self):
 45       gtk.main()
 46   
 47     def destroy( self, w):
 48       print "Good bye..."
 49       gtk.main_quit()
 50   
 51     def button1_clicked( self, w):
 52       self.label.set_label( "You clicked the right button")
 53   
 54     def red_button_clicked( self, w):
 55       self.label.set_label( "Please don't press this button again")
 56   
 57   
 58   if __name__ == "__main__":
 59     m = MyGUI( "Buttons")
 60     m.main()
stdout:
Good bye...
Screenshot:
Program screenshot gtk4-1.pngProgram screenshot gtk4-1a.pngProgram screenshot gtk4-1b.png
Doba běhu: 2256.9 ms
Buttons can have a keyboard shortcut associated with them. To add this so called mnemonics prepend one letter of the button label with an underscore ("_") and add the "use_underline" argument to the Button constructor. After doing so, the button may be activated using the combination "Alt"+[the underscored key].
Expand/Shrink
Rozdíl proti: gtk4-1.py
@@ -28,12 +28,12 @@
     self.button_box = gtk.HBox()
     self.mainbox.pack_end( self.button_box)
     # first button
-    button = gtk.Button( "Press me")
+    button = gtk.Button( "_Press me", use_underline=True)
     button.connect( "clicked", self.button1_clicked)
     self.button_box.pack_start( button)
     button.show()
     # second button
-    button = gtk.Button( "Big red button")
+    button = gtk.Button( "Big _red button", use_underline=True)
     button.connect( "clicked", self.red_button_clicked)
     self.button_box.pack_start( button)
     button.show()
Zdroj: (gtk4-2.py) +++
stdout:
Good bye...
Screenshot:
Program screenshot gtk4-2.png
Doba běhu: 3919.8 ms