Opengl touch events
In this program we create 4 custom signals, the first to are connected to and triggered on launch, the latter two are triggered when you click the buttons.
We can create new signals using GObject.signalnew, we can then connect the new signal and callback as we normally would using widget .connect methods as normal. To trigger our custom event we use the .emit() method, this method also aalows us to fake events, for example we might want to fake the user clicking a button.
Gtk signalsView python code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
#!/usr/bin/python
from gi.repository import Gtk
from gi.repository import GObject
class application_gui:
"""Tutorial 02 buttons"""
def __init__(self):
#load in our glade interface
xml = Gtk.Builder()
xml.add_from_file('tut16-signals.glade')
#grab our widget using get_object this is the name of the widget from glade, window1 is the default name
self.window = xml.get_object('window1')
self.text = xml.get_object('entry1')
GObject.signal_new('custom-signal1', self.window, GObject.SIGNAL_RUN_LAST, GObject.TYPE_PYOBJECT, (GObject.TYPE_PYOBJECT,))
GObject.signal_new('custom-signal2', self.window, GObject.SIGNAL_RUN_LAST, GObject.TYPE_PYOBJECT, (GObject.TYPE_PYOBJECT,))
self.window.connect('custom-signal1', self.custom_signal1_method)
self.window.connect('custom-signal2', self.custom_signal2_method)
#emit the custom signals on launch.
self.window.emit('custom-signal1', 'hello from signal1')
self.window.emit('custom-signal2', None)
self.buttons = {}
self.buttons['but1'] = xml.get_object('button1')
self.buttons['but2'] = xml.get_object('button2')
self.buttons['but3'] = xml.get_object('togglebutton1')
self.buttons['but4'] = xml.get_object('togglebutton2')
GObject.signal_new('custom-signal3', self.buttons['but1'], GObject.SIGNAL_RUN_LAST, GObject.TYPE_PYOBJECT, (GObject.TYPE_PYOBJECT,))
GObject.signal_new('custom-signal4', self.buttons['but2'], GObject.SIGNAL_RUN_LAST, GObject.TYPE_PYOBJECT, (GObject.TYPE_PYOBJECT,))
self.buttons['but1'].connect('clicked', self.button_events)
self.buttons['but1'].connect('custom-signal3', self.custom_signal3_method)
self.buttons['but2'].connect('clicked', self.button_events)
self.buttons['but2'].connect('custom-signal4', self.custom_signal4_method)
self.buttons['but3'].connect('clicked', self.button_events)
self.buttons['but4'].connect('clicked', self.button_events)
#fake the user clicking one of our buttons
self.buttons['but1'].emit('clicked')
#connect to events, in this instance just quit our application
self.window.connect('delete_event', Gtk.main_quit)
self.window.connect('destroy', lambda quit: Gtk.main_quit())
#show the window else there is nothing to see :)
self.window.show()
def custom_signal1_method(self, *args):
print('custom_signal1 emitted')
print(args)
def custom_signal2_method(self, *args):
print('custom_signal2 emitted')
print(args)
def custom_signal3_method(self, *args):
print('custom_signal3 emitted')
print(args)
def custom_signal4_method(self, *args):
print('custom_signal4 emitted')
print(args)
def button_events(self, widget):
widget.emit('custom-signal3', ('1', '2', '3'))
toggle_value = ''
if widget.get_name() == 'GtkToggleButton':
toggle_value = str(widget.get_active())
self.text.set_text(widget.get_name() + ' ' +widget.get_label()+ ' ' + toggle_value)
application = application_gui()
Gtk.main()
|
View glade XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.0 -->
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkGrid" id="grid1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkButton" id="button1">
<property name="label" translatable="yes">Button One Standard Button</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
<property name="width">1</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkToggleButton" id="togglebutton1">
<property name="label" translatable="yes">Button Three Toggle Button</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
<property name="width">1</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkToggleButton" id="togglebutton2">
<property name="label" translatable="yes">Button Four Toggle Button</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">1</property>
<property name="width">1</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button2">
<property name="label" translatable="yes">Button Two Standard Button</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
<property name="width">1</property>
<property name="height">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="invisible_char">•</property>
<property name="input_purpose">alpha</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
|