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

<-^^->

Progressbar

Widget displaying progress of some process.
Expand/Shrink
Zdroj: (gtk-progressbar1-1.py) přispěl(a) Kryštof Dibusz
  1   import pygtk
  2   pygtk.require('2.0')
  3   import gtk, gobject
  4   
  5   class MyGUI:
  6   
  7       def __init__( self, title):
  8           ## cannot see difference between gtk.Window()?
  9           self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
 10   
 11           ## resize window 
 12           self.window.set_resizable(True)
 13   
 14           ## inner window borders
 15           self.window.set_border_width(10)        
 16           self.window.set_title( title)
 17           self.window.connect( "destroy", self.destroy)
 18   
 19           self.create_interior()
 20           self.window.show_all()
 21           
 22       def create_interior( self):
 23           self.mainbox = gtk.VBox(False, 5)
 24           self.mainbox.set_border_width(15)
 25           self.window.add(self.mainbox)
 26           self.mainbox.show()
 27   
 28           ## Create a centering alignment object
 29           ## expand and fill must be True!
 30           ## first argument if 1.0/0.0/0.5 = stick to right/left/middle
 31           ## second argument if 1.0/0.0/0.5 = stick to bottom/top/middle
 32           ## third argument - height percentage
 33           ## fourth argument - width percentage
 34           align = gtk.Alignment(0.2, 0.1, 0.7, 0.3)
 35           self.mainbox.pack_start(align, True, True, 5)
 36           align.show()
 37   
 38           ## Create the ProgressBar
 39           self.pbar = gtk.ProgressBar() 
 40           align.add(self.pbar) 
 41           self.pbar.show() 
 42   
 43           # Add a timer callback to update the value of the progress bar
 44           ## That's the hack :)
 45           self.timer = gobject.timeout_add (100, self.progress_timeout) 
 46   
 47           ## similar to menu separator, this separetes objects in mainbox
 48           separator = gtk.HSeparator()
 49           self.mainbox.pack_start(separator, False, False,10)
 50           separator.show()
 51   
 52           # rows, columns, homogeneous
 53           table = gtk.Table(2, 2, False)
 54           self.mainbox.pack_start(table, False, False, 0)
 55           table.show()
 56   
 57           # Add a check button to select displaying of the trough text
 58           check = gtk.CheckButton("Show text")
 59           table.attach(check, 0, 1, 0, 1)
 60           check.connect("clicked", self.toggle_show_text, "SOME TEXT")
 61           check.show()        
 62           
 63           # Add a check button to toggle activity mode
 64           # Interesting shortcut (check)
 65           self.activity_check = check = gtk.CheckButton("Activity mode")
 66           table.attach(check, 0, 1, 1, 2)
 67           check.connect("clicked", self.toggle_activity_mode)
 68           check.show()
 69   
 70           # Add a check button to toggle orientation
 71           check = gtk.CheckButton("Right to Left")
 72           table.attach(check, 0, 1, 2, 3)
 73           check.connect("clicked", self.toggle_orientation)
 74           check.show()
 75           
 76           # Add a button to exit the program
 77           button = gtk.Button("exit")
 78           button.connect("clicked", self.destroy)
 79           self.mainbox.pack_start(button, False, False, 0)
 80           # This makes it so the button is the default.
 81           button.set_flags(gtk.CAN_DEFAULT) 
 82           # This grabs this button to be the default button. Simply hitting
 83           # the "Enter" key will cause this button to activate.
 84           button.grab_default() 
 85           button.show()
 86   
 87       def progress_timeout(self):
 88           if self.activity_check.get_active():
 89               self.pbar.pulse()
 90    
 91           else:
 92               # Calculate the value of the progress bar using the
 93               # value range set in the adjustment object
 94               new_val = self.pbar.get_fraction() + 0.02
 95               if new_val > 0.75:
 96                   new_val = 0.0
 97                   
 98               # Set the new value
 99               self.pbar.set_fraction(new_val)
100   
101           # As this is a timeout function, return TRUE so that it
102           # continues to get called
103           return True 
104   
105       # Callback that toggles the text display within the progress
106       # bar trough
107       def toggle_show_text(self, w, data=None):
108           if w.get_active():
109               self.pbar.set_text(data)
110           else:
111               self.pbar.set_text("")
112   
113       # Callback that toggles the activity mode of the progress
114       # bar
115       def toggle_activity_mode(self, w, data=None):
116           if w.get_active():
117               self.pbar.set_pulse_step(0.1)
118               #pass
119               
120           else:
121               self.pbar.set_fraction(0.20)
122   
123       # Callback that toggles the orientation of the progress bar
124       def toggle_orientation(self, widget, data=None):
125           if self.pbar.get_orientation() == gtk.PROGRESS_LEFT_TO_RIGHT:
126               self.pbar.set_orientation(gtk.PROGRESS_RIGHT_TO_LEFT)
127           elif self.pbar.get_orientation() == gtk.PROGRESS_RIGHT_TO_LEFT:
128               self.pbar.set_orientation(gtk.PROGRESS_LEFT_TO_RIGHT)
129   
130       # Clean up allocated memory, remove the timer and quit
131       def destroy(self, w, data=None):
132           gobject.source_remove(self.timer)
133           self.timer = 0
134           gtk.main_quit()
135           
136       #def destroy( self, w):
137        #   gtk.main_quit()
138   
139   def main():
140       gtk.main()
141       return 0
142   
143   if __name__ == "__main__":
144       MyGUI("Progress Bar")
145       main()
146       
Screenshot:
Program screenshot gtk-progressbar1-1.pngProgram screenshot gtk-progressbar1-1a.pngProgram screenshot gtk-progressbar1-1b.png
Doba běhu: 1039.7 ms