Python-Ref > GUI programming with PyGTK > Dialogs > Dialog
 
 

^^->

Dialog

General dialog widget.
When we need to create our own dialogs, we can use the general Dialog widget which provides an empty dialog window. We can than fill the window with whatever we need.
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       # a label
 21       self.label = gtk.Label()
 22       self.label.set_size_request( -1, 40)
 23       self.mainbox.pack_start( self.label)
 24       self.label.show()
 25       # button box
 26       button_box = gtk.HBox()
 27       self.mainbox.pack_start( button_box, expand=False)
 28       button_box.show()
 29       # button 1
 30       b1 = gtk.Button( "Dialog 1")
 31       button_box.pack_start( b1, expand=False)
 32       b1.show()
 33       b1.connect( "clicked", self.dialog_1)
 34       # button 2
 35       b2 = gtk.Button( "Dialog 2")
 36       button_box.pack_start( b2, expand=False)
 37       b2.show()
 38       b2.connect( "clicked", self.dialog_2)
 39       self.mainbox.show()
 40   
 41     def dialog_1( self, w):
 42       d = gtk.Dialog( title="Dialog 1",
 43                       parent=self.window,
 44                       flags=gtk.DIALOG_DESTROY_WITH_PARENT,
 45                       buttons=("OK",True,"Cancel",False))
 46       l = gtk.Label( "Select OK or Cancel below, it does not matter.")
 47       l.set_padding( 10, 10)
 48       d.vbox.pack_start( l)
 49       l.show()
 50       result = d.run()
 51       d.destroy()
 52       self.label.set_markup( "Last response: <b>%s</b>" % result)
 53   
 54     def dialog_2( self, w):
 55       d = gtk.Dialog( title="Dialog 2",
 56                       parent=self.window,
 57                       flags=gtk.DIALOG_DESTROY_WITH_PARENT|gtk.DIALOG_MODAL,
 58                       buttons=(gtk.STOCK_SAVE, 1,
 59                                "Discard", 2,
 60                                gtk.STOCK_CANCEL, 3))
 61       l = gtk.Label( "Unsaved changes found. What do you want to do?")
 62       l.set_padding( 10, 10)
 63       d.vbox.pack_start( l)
 64       l.show()
 65       d.set_response_sensitive( 1, False) 
 66       d.set_default_response( 3) 
 67       result = d.run()
 68       d.destroy()
 69       self.label.set_markup( "Last response: <b>%s</b>" % result)
 70   
 71     def main( self):
 72       gtk.main()
 73   
 74     def destroy( self, w):
 75       gtk.main_quit()
 76   
 77   
 78   if __name__ == "__main__":
 79     m = MyGUI( "Dialog example")
 80     m.main()
Screenshot:
Program screenshot gtk-dialog1-1.pngProgram screenshot gtk-dialog1-1a.pngProgram screenshot gtk-dialog1-1b.pngProgram screenshot gtk-dialog1-1c.png
Doba běhu: 2100.8 ms
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       # a label
 21       self.label = gtk.Label()
 22       self.label.set_size_request( -1, 40)
 23       self.mainbox.pack_start( self.label)
 24       self.label.show()
 25       # button box
 26       button_box = gtk.HBox()
 27       self.mainbox.pack_start( button_box, expand=False)
 28       button_box.show()
 29       # button 1
 30       b1 = gtk.Button( "Dialog 1")
 31       button_box.pack_start( b1, expand=False)
 32       b1.show()
 33       b1.connect( "clicked", self.dialog_1)
 34       # button 2
 35       b2 = gtk.Button( "Dialog 2")
 36       button_box.pack_start( b2, expand=False)
 37       b2.show()
 38       b2.connect( "clicked", self.dialog_2)
 39       self.mainbox.show()
 40   
 41     def dialog_1( self, w):
 42       d = gtk.Dialog( title="Give me your name",
 43                       parent=self.window,
 44                       flags=gtk.DIALOG_DESTROY_WITH_PARENT,
 45                       buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
 46                                gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
 47       box = gtk.HBox()
 48       d.vbox.pack_start( box)
 49       box.show()
 50       l = gtk.Label( "Give me your name:")
 51       box.pack_start( l)
 52       l.show()
 53       entry = gtk.Entry()
 54       box.pack_start( entry)
 55       entry.show()
 56       # the following will trigger OK response when enter is hit in the entry
 57       entry.connect( "activate", lambda w: d.response( gtk.RESPONSE_OK)) 
 58       # run the dialog
 59       result = d.run()
 60       d.destroy()
 61       if result == gtk.RESPONSE_OK:
 62         text = "Your name is <b>%s</b>" % entry.get_text()
 63       else:
 64         text = "Dialog was canceled"
 65       self.label.set_markup( text)
 66   
 67     def dialog_2( self, w):
 68       d = gtk.Dialog( title="Select your meal",
 69                       parent=self.window,
 70                       flags=gtk.DIALOG_DESTROY_WITH_PARENT|gtk.DIALOG_NO_SEPARATOR,
 71                       buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
 72                                gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
 73       f = gtk.Frame( "Select your meal") 
 74       d.vbox.pack_start( f)
 75       f.show()
 76       # box for radio buttons
 77       box = gtk.VBox()
 78       f.add( box)
 79       box.show()
 80       first = None
 81       for text in ["Pork ribs","Beef steak","Vegetable salad"]:
 82         b = gtk.RadioButton( first, text)
 83         if not first:
 84           first = b
 85         box.pack_start( b, expand=False)
 86         b.show()
 87       d.set_default_response( gtk.RESPONSE_OK)
 88       result = d.run()
 89       if result == gtk.RESPONSE_OK:
 90         active = [radio for radio in b.get_group() if radio.get_active()] 
 91         text = "You selected <b>%s</b>." % active[0].get_label() 
 92       else:
 93         text = "You will go hungry."
 94       d.destroy()
 95       self.label.set_markup( text)
 96   
 97     def main( self):
 98       gtk.main()
 99   
100     def destroy( self, w):
101       gtk.main_quit()
102   
103   
104   if __name__ == "__main__":
105     m = MyGUI( "Dialog example")
106     m.main()
Screenshot:
Program screenshot gtk-dialog1-2.pngProgram screenshot gtk-dialog1-2a.pngProgram screenshot gtk-dialog1-2b.pngProgram screenshot gtk-dialog1-2c.png
Doba běhu: 3761.4 ms