Last active
August 29, 2015 14:27
-
-
Save campaul/3508998e67f93403cbc1 to your computer and use it in GitHub Desktop.
Off main thread GTK example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Inverted version of the first example from https://wiki.gnome.org/Projects/PyGObject/Threading | |
# GTK runs in the spawned thread instead of the other way around. | |
import threading | |
import time | |
from gi.repository import GLib, Gtk, GObject | |
# yay hacks | |
registry = {} | |
def app_main(): | |
win = Gtk.Window(default_height=50, default_width=300) | |
win.connect("delete-event", Gtk.main_quit) | |
progress = Gtk.ProgressBar(show_text=True) | |
win.add(progress) | |
registry['progress'] = progress | |
win.show_all() | |
if __name__ == "__main__": | |
def start_gtk(): | |
app_main() | |
Gtk.main() | |
thread = threading.Thread(target=start_gtk) | |
thread.daemon = True | |
thread.start() | |
def update_progess(i): | |
progress = registry['progress'] | |
progress.pulse() | |
progress.set_text(str(i)) | |
return False | |
for i in range(50): | |
GLib.idle_add(update_progess, i) | |
time.sleep(0.2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment