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

<-^^->

Statusbar

Message display widget with history tracking.
Expand/Shrink
Zdroj: (gtk-statusbar1-1.py) přispěl(a) Tom Jankovec
  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   
  8   class MyGUI:
  9   
 10       def __init__( self, title):
 11           self.window = gtk.Window()
 12           self.title = title
 13           self.window.set_title( title)
 14           self.window.set_size_request( 300, 200)
 15           self.window.connect( "destroy", self.destroy)
 16           self.create_interior()
 17           self.window.show_all()
 18           
 19       def create_interior( self):
 20           self.mainbox = gtk.VBox()
 21           self.window.add( self.mainbox)
 22           # defining statusbar
 23           self.status_bar = gtk.Statusbar()   
 24           self.status_bar.show()
 25           # resize grip - small area in lower right corner for window resizing
 26           self.status_bar.set_has_resize_grip( False)
 27           # context identifier
 28           self.context_id = self.status_bar.get_context_id( "Statusbar example")
 29           # push() method pushes a new message with the specified context_id
 30           # onto a statusbar's stack 
 31           self.status_bar.push( self.context_id, "Welcome to menu!")
 32           # menu
 33           label_menu = gtk.Menu()
 34           load_file_item = gtk.MenuItem( "Load file")
 35           load_file_item.connect( "activate", self.load_file, self.context_id)
 36           load_file_item.show()
 37           undo_item = gtk.MenuItem( "Undo")
 38           undo_item.connect( "activate", self.undo,self.context_id)
 39           undo_item.show()
 40           run_item = gtk.MenuItem( "Run program")
 41           run_item.connect( "activate", self.run_function, self.context_id)
 42           run_item.show()
 43           label_menu.append( load_file_item)
 44           label_menu.append( undo_item)
 45           label_menu.append( run_item)
 46           # menu bar
 47           self.menu_bar = gtk.MenuBar()
 48           label_menu_item = gtk.MenuItem( "File")
 49           label_menu_item.set_submenu( label_menu)
 50           self.menu_bar.append( label_menu_item)
 51           self.menu_bar.show()
 52           self.empty_box=gtk.VBox()
 53           # packing
 54           self.mainbox.pack_start( self.menu_bar, expand=False, fill=False)
 55           self.mainbox.pack_start(self.empty_box)
 56           self.mainbox.pack_start( self.status_bar, expand=False)
 57           self.mainbox.show()
 58       
 59       def main( self):
 60           gtk.main()
 61       
 62       def destroy( self, w):
 63           gtk.main_quit()
 64           
 65       def load_file( self, w, data):
 66           self.status_bar.push( data, "Choose a file!")
 67           d = gtk.FileChooserDialog( title="Load a file",
 68                                  parent=self.window,
 69                                  action=gtk.FILE_CHOOSER_ACTION_OPEN,
 70                                  buttons=("OK",True,"Cancel",False)
 71                                  )
 72           ok = d.run()
 73           if ok:
 74               fullname = d.get_filename()
 75               self.status_bar.push( data, fullname)
 76           else:
 77               self.status_bar.push( data, "File dialog has been canceled.")
 78           d.destroy()
 79   
 80       def run_function( self, w, data):
 81           value = "This is some text from run_function."
 82           self.status_bar.push( data, value)
 83           
 84       def undo( self, w, data):
 85           # pop() method removes the top message with the specified
 86           # context_id from the statusbar's stack.
 87           self.status_bar.pop( data)
 88           
 89   if __name__ == "__main__":
 90       m = MyGUI( "Statusbar example" )
 91       m.main()
 92       
 93       
 94       
Screenshot:
Program screenshot gtk-statusbar1-1.pngProgram screenshot gtk-statusbar1-1a.png
Doba běhu: 1035.6 ms