Python-Ref > GUI programming with PyGTK > Events > Events
 
 

^^
Klíčová slova
Moduly
Knihovní funkce

Events

How to react to user actions.
Expand/Shrink
Zdroj: (gtk7-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.HBox()
 19       self.window.add( self.mainbox)
 20       # first button
 21       button = gtk.Button( "Press me")
 22       button.connect( "clicked", self.button_clicked)
 23       self.mainbox.pack_start( button)
 24       button.show()
 25       # second button
 26       button = gtk.Button( "Big red button")
 27       button.connect( "clicked", self.button_clicked)
 28       self.mainbox.pack_start( button)
 29       button.show()
 30       # show the box
 31       self.mainbox.show()
 32   
 33     def main( self):
 34       gtk.main()
 35   
 36     def destroy( self, w):
 37       gtk.main_quit()
 38   
 39     def button_clicked( self, w):
 40       print "You pressed:", w.get_label()
 41   
 42   
 43   if __name__ == "__main__":
 44     m = MyGUI( "Hello Boxes")
 45     m.main()
stdout:
You pressed: Press me
You pressed: Big red button
Screenshot:
Program screenshot gtk7-1.png
Doba běhu: 13735.2 ms
Expand/Shrink
Rozdíl proti: gtk7-1.py
@@ -19,12 +19,12 @@
     self.window.add( self.mainbox)
     # first button
     button = gtk.Button( "Press me")
-    button.connect( "clicked", self.button_clicked)
+    button.connect( "clicked", self.button_clicked, 1)
     self.mainbox.pack_start( button)
     button.show()
     # second button
     button = gtk.Button( "Big red button")
-    button.connect( "clicked", self.button_clicked)
+    button.connect( "clicked", self.button_clicked, 2)
     self.mainbox.pack_start( button)
     button.show()
     # show the box
@@ -36,8 +36,8 @@
   def destroy( self, w):
     gtk.main_quit()
-  def button_clicked( self, w):
-    print "You pressed:", w.get_label()
+  def button_clicked( self, w, data=None):
+    print "You pressed button:", data
 if __name__ == "__main__":
Zdroj: (gtk7-2.py) +++
stdout:
You pressed button: 1
You pressed button: 2
Screenshot:
Program screenshot gtk7-2.png
Doba běhu: 9805.8 ms