| 1 |
import gi |
| 2 |
|
| 3 |
gi.require_version("Gtk", "4.0") |
| 4 |
from gi.repository import Gtk, GLib |
| 5 |
|
| 6 |
|
| 7 |
class ProgressBarWindow(Gtk.ApplicationWindow): |
| 8 |
def __init__(self, **kargs): |
| 9 |
super().__init__(**kargs, title="ProgressBar Demo") |
| 10 |
|
| 11 |
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6) |
| 12 |
self.set_child(vbox) |
| 13 |
|
| 14 |
self.progressbar = Gtk.ProgressBar() |
| 15 |
vbox.append(self.progressbar) |
| 16 |
|
| 17 |
button = Gtk.CheckButton(label="Show text") |
| 18 |
button.connect("toggled", self.on_show_text_toggled) |
| 19 |
vbox.append(button) |
| 20 |
|
| 21 |
button = Gtk.CheckButton(label="Activity mode") |
| 22 |
button.connect("toggled", self.on_activity_mode_toggled) |
| 23 |
vbox.append(button) |
| 24 |
|
| 25 |
button = Gtk.CheckButton(label="Right to Left") |
| 26 |
button.connect("toggled", self.on_right_to_left_toggled) |
| 27 |
vbox.append(button) |
| 28 |
|
| 29 |
self.timeout_id = GLib.timeout_add(50, self.on_timeout) |
| 30 |
self.activity_mode = False |
| 31 |
|
| 32 |
def on_show_text_toggled(self, button): |
| 33 |
show_text = button.props.active |
| 34 |
text = "some text" if show_text else None |
| 35 |
self.progressbar.props.text = text |
| 36 |
self.progressbar.props.show_text = show_text |
| 37 |
|
| 38 |
def on_activity_mode_toggled(self, button): |
| 39 |
self.activity_mode = button.props.active |
| 40 |
if self.activity_mode: |
| 41 |
self.progressbar.pulse() |
| 42 |
else: |
| 43 |
self.progressbar.props.fraction = 0.0 |
| 44 |
|
| 45 |
def on_right_to_left_toggled(self, button): |
| 46 |
value = button.props.active |
| 47 |
self.progressbar.props.inverted = value |
| 48 |
|
| 49 |
def on_timeout(self): |
| 50 |
"""Update value on the progress bar.""" |
| 51 |
if self.activity_mode: |
| 52 |
self.progressbar.pulse() |
| 53 |
else: |
| 54 |
new_value = self.progressbar.props.fraction + 0.01 |
| 55 |
|
| 56 |
if new_value > 1: |
| 57 |
new_value = 0 |
| 58 |
|
| 59 |
self.progressbar.props.fraction = new_value |
| 60 |
|
| 61 |
# As this is a timeout function, return True so that it |
| 62 |
# continues to get called |
| 63 |
return True |
| 64 |
|
| 65 |
|
| 66 |
def on_activate(app): |
| 67 |
win = ProgressBarWindow(application=app) |
| 68 |
win.present() |
| 69 |
|
| 70 |
|
| 71 |
app = Gtk.Application(application_id="com.example.App") |
| 72 |
app.connect("activate", on_activate) |
| 73 |
|
| 74 |
app.run(None) |