ProgressBar (Python) Marta Maria Casetti mmcasetti@gmail.com 2012 Ένα γραφικό στοιχείο που δείχνει την πρόοδο οπτικά. Ελληνική μεταφραστική ομάδα GNOME team@gnome.gr 2012-2015 Δημήτρης Σπίγγος dmtrs32@gmail.com 2012, 2013 Μαρία Θουκιδίδου marablack3@gmail.com 2014 Θάνος Τρυφωνίδης tomtryf@gmail.com 2014, 2015 Γραμμή προόδου (ProgressBar) Πατώντας οποιοδήποτε πλήκτρο σταματά και αρχίζει αυτή τη ProgressBar.

Αυτή η ProgressBar σταματά και ξεκινά πατώντας οποιοδήποτε πλήκτρο.

Ο χρησιμοποιούμενος κώδικας για παραγωγή αυτού παραδείγματος from gi.repository import GLib from gi.repository import Gtk import sys class MyWindow(Gtk.ApplicationWindow): # a window def __init__(self, app): Gtk.Window.__init__(self, title="ProgressBar Example", application=app) self.set_default_size(220, 20) # a progressbar self.progress_bar = Gtk.ProgressBar() # add the progressbar to the window self.add(self.progress_bar) # the method self.pulse is called each 100 milliseconds # and self.source_id is set to be the ID of the event source # (i.e. the bar changes position every 100 milliseconds) self.source_id = GLib.timeout_add(100, self.pulse) # event handler # any signal from the keyboard controls if the progressbar stops/starts def do_key_press_event(self, event): # if the progressbar has been stopped (therefore source_id == 0 - see # "else" below), turn it back on if (self.source_id == 0): self.source_id = GLib.timeout_add(100, self.pulse) # if the bar is moving, remove the source with the ID of source_id # from the main context (stop the bar) and set the source_id to 0 else: GLib.source_remove(self.source_id) self.source_id = 0 # stop the signal emission return True # source function # the progressbar is in "activity mode" when this method is called def pulse(self): self.progress_bar.pulse() # call the function again return True class MyApplication(Gtk.Application): def __init__(self): Gtk.Application.__init__(self) def do_activate(self): win = MyWindow(self) win.show_all() def do_startup(self): Gtk.Application.do_startup(self) app = MyApplication() exit_status = app.run(sys.argv) sys.exit(exit_status)
Χρήσιμες μέθοδοι για ένα γραφικό στοιχείο ProgressBar

Αντί για pulse(), που κάνει τη γραμμή να πηγαίνει μπροστά πίσω, εάν θέλουμε η ProgressBar να "γεμίζει" ένα κλάσμα (ένα float μεταξύ 0.0 και 1.0 συμπεριλαμβανόμενου) της γραμμής που έχει ολοκληρωθεί, χρησιμοποιήστε set_fraction(fraction).

Για ρύθμιση κειμένου και εμφάνισή του (υπερτιθέμενου της γραμμής) χρησιμοποιήστε set_text("text") και set_show_text(True). Εάν ένα κείμενο δεν ορίζεται και το set_show_text(True), τότε το κείμενο θα είναι το ποσοστό της εργασίας που έχει ολοκληρωθεί.

Αναφορές API

Σε αυτό το παράδειγμα χρησιμοποιήσαμε τα παρακάτω:

GtkProgressBar

GLib - The Main Event Loop

Gdk - Key Values