Created
March 10, 2018 00:52
-
-
Save andy0130tw/70d94c4cb39e7330dc8642215243b048 to your computer and use it in GitHub Desktop.
PyGObject examples used in SITCON 2018.
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
import sys | |
import gi | |
gi.require_version('Gtk', '3.0') | |
from gi.repository import Gtk, Gio | |
class Application(Gtk.Application): | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
self.window = None | |
def do_startup(self, *args): | |
Gtk.Application.do_startup(self) | |
self.connect('activate', self.my_activate) | |
def my_activate(self, *args): | |
window = Gtk.ApplicationWindow(application=self) | |
window.set_title('Window') | |
window.set_default_size(200, 200) | |
window.present() | |
if __name__ == '__main__': | |
import signal | |
signal.signal(signal.SIGINT, signal.SIG_DFL) | |
app = Application(application_id='org.gtk.example-py', | |
flags=Gio.ApplicationFlags.FLAGS_NONE) | |
app.run(sys.argv) |
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
import sys | |
import gi | |
gi.require_version('Gtk', '3.0') | |
from gi.repository import Gtk, Gio | |
class Application(Gtk.Application): | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
self.builder = None | |
def do_startup(self, *args): | |
Gtk.Application.do_startup(self) | |
builder = Gtk.Builder() | |
builder.add_from_file('01a-ui.glade') | |
builder.connect_signals(self) | |
self.builder = builder | |
def do_activate(self, *args): | |
window = self.builder.get_object('app_window') | |
self.add_window(window) | |
window.show() | |
def onButtonClick(self, *args): | |
print('owo') | |
if __name__ == '__main__': | |
import signal | |
signal.signal(signal.SIGINT, signal.SIG_DFL) | |
app = Application(application_id='org.gtk.example-py-glade', | |
flags=Gio.ApplicationFlags.FLAGS_NONE) | |
app.run(sys.argv) |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<!-- Generated with glade 3.20.2 --> | |
<interface> | |
<requires lib="gtk+" version="3.20"/> | |
<object class="GtkApplicationWindow" id="app_window"> | |
<property name="can_focus">False</property> | |
<property name="default_width">440</property> | |
<property name="default_height">250</property> | |
<child> | |
<object class="GtkButton"> | |
<property name="label" translatable="yes">Just a button owo</property> | |
<property name="visible">True</property> | |
<property name="can_focus">True</property> | |
<property name="receives_default">True</property> | |
<signal name="clicked" handler="onButtonClick" swapped="no"/> | |
</object> | |
</child> | |
<child> | |
<placeholder/> | |
</child> | |
</object> | |
</interface> |
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
import sys | |
import threading | |
import time | |
import gi | |
gi.require_version('Gtk', '3.0') | |
from gi.repository import Gtk, Gio, GLib | |
class BackgroundThread(threading.Thread): | |
def __init__(self, app): | |
super().__init__(target=self.counter) | |
self.daemon = True | |
def counter(self): | |
cnt = 0 | |
while True: | |
if app.window: | |
GLib.idle_add(app.window.set_title, (str(cnt))) | |
cnt += 1 | |
time.sleep(0.1) | |
class Application(Gtk.Application): | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
self.window = None | |
def do_startup(self, *args): | |
Gtk.Application.do_startup(self) | |
self.connect('activate', self.my_activate) | |
def my_activate(self, *args): | |
window = Gtk.ApplicationWindow(application=self) | |
window.set_title('Window') | |
window.set_default_size(200, 200) | |
window.present() | |
self.window = window | |
if __name__ == '__main__': | |
import signal | |
signal.signal(signal.SIGINT, signal.SIG_DFL) | |
app = Application(application_id='org.gtk.example-py-thread', | |
flags=Gio.ApplicationFlags.FLAGS_NONE) | |
thr = BackgroundThread(app) | |
thr.start() | |
app.run(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment