-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.py
73 lines (58 loc) · 2.46 KB
/
builder.py
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
import inspect
from gi.repository import GObject,Gtk
from xml.etree.cElementTree import ElementTree
class builder():
def __init__(self,filename,window):
self.widgets = {}
self.widgets = {}
self.builder = Gtk.Builder()
self.builder.add_from_file(filename)
self.ShowWindow(window)
tree = ElementTree()
tree.parse(filename)
ele_widgets = tree.getiterator("object")
for ele_widget in ele_widgets:
name = ele_widget.attrib['id']
widget = self.builder.get_object(name)
self.widgets[name] = widget
def ShowWindow(self,window):
self.builder.get_object(window).show()
def get_ui(self, callback_obj):
'''Return the attributes of the widgets and Connects to Handler'''
#Get all methods of class window and write them to a dictionary
methods = []
for k in dir(callback_obj):
try:
attr = getattr(callback_obj, k)
except:
continue
if inspect.ismethod(attr):
methods.append((k, attr))
methods.sort()
dict_methods = dict(methods)
callback_handler_dict = {}
callback_handler_dict.update(dict_methods)
for (widget_name, widget) in self.widgets.items():
setattr(self, widget_name, widget) #Return the attribute
signal_names = self.__get_signals(widget) #Get a list of the widget signals
for signal_name in signal_names:
signal_name = signal_name.replace("-", "_")
handler_names = ["on_%s_%s" % (widget_name, signal_name)]
#For every method of the window, connect the signals
for handler_name in handler_names:
target = handler_name in callback_handler_dict.keys()
if target:
widget.connect(signal_name, callback_handler_dict[handler_name])
return self
def __get_signals(self, widget):
'''Return the signals of the widget'''
signal_ids = []
try:
widget_type = type(widget)
while widget_type:
signal_ids.extend(GObject.signal_list_ids(widget_type))
widget_type = GObject.type_parent(widget_type)
except RuntimeError:
pass
signal_names = [GObject.signal_name(sid) for sid in signal_ids]
return signal_names