Python-Ref > GUI programming with PyGTK > Dialogs > Message dialog
 
 

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

Message dialog

Dialog for messages.
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.MessageDialog( parent=self.window,
 43                              type=gtk.MESSAGE_INFO,
 44                              flags=gtk.DIALOG_DESTROY_WITH_PARENT,
 45                              buttons=gtk.BUTTONS_OK,
 46                              message_format="Hello world! This is message dialog example"
 47                              )
 48       result = d.run()
 49       d.destroy()
 50       self.label.set_markup( "Last response: <b>%s</b>" % result)
 51   
 52     def dialog_2( self, w):
 53       d = gtk.MessageDialog( parent=self.window,
 54                              type=gtk.MESSAGE_ERROR,
 55                              flags=gtk.DIALOG_DESTROY_WITH_PARENT|gtk.DIALOG_MODAL,
 56                              buttons=gtk.BUTTONS_CLOSE,
 57                              message_format="A critical error has occured!"
 58                              )
 59       result = d.run()
 60       d.destroy()
 61       self.label.set_markup( "Last response: <b>%s</b>" % result)
 62   
 63     def main( self):
 64       gtk.main()
 65   
 66     def destroy( self, w):
 67       gtk.main_quit()
 68   
 69   
 70   if __name__ == "__main__":
 71     m = MyGUI( "MessageDialog example")
 72     m.main()
Screenshot:
Program screenshot gtk-dialog2-1.pngProgram screenshot gtk-dialog2-1a.pngProgram screenshot gtk-dialog2-1b.pngProgram screenshot gtk-dialog2-1c.png
Doba běhu: 1352.0 ms