Skip to content

Instantly share code, notes, and snippets.

@sulincix
Last active July 17, 2024 08:32
Show Gist options
  • Save sulincix/4b3b472f01ad1e1b113244f5abb982d5 to your computer and use it in GitHub Desktop.
Save sulincix/4b3b472f01ad1e1b113244f5abb982d5 to your computer and use it in GitHub Desktop.
python gtk async
#!/usr/bin/env python3
import gi
# gtk import
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GLib
# thread import
import threading
# for sleep
import time
# gui elements
w = Gtk.Window()
l = Gtk.Label()
# blocking function
def count():
num=0
while True:
# gtk related functions must run with GLib.idle_add
GLib.idle_add(l.set_label, str(num))
time.sleep(1)
num=num+1
# thread create and start
thread = threading.Thread(target=count)
thread.daemon = True
thread.start()
# gui signals
w.connect("destroy", Gtk.main_quit)
w.add(l)
w.show_all()
# start
Gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment