/
widgets.py
298 lines (246 loc) · 9.04 KB
/
widgets.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"""
Test program for trying widgets in the different wrapped toolkits supported
by Ginga.
Usage:
$ python widgets.py <toolkit-name> <widget-name> [logging options]
Examples:
$ python widgets.py qt5 gridbox
$ python widgets.py gtk3 label
$ python widgets.py pg button
"""
import sys
import os
from ginga.misc import log
import ginga.toolkit as ginga_toolkit
# decide our toolkit, then import
tkit = sys.argv[1]
wname = sys.argv[2]
ginga_toolkit.use(tkit)
from ginga.gw import Widgets, Viewers, GwHelp # noqa
from ginga.util.paths import icondir # noqa
top = None
def quit(*args):
if top is not None:
top.delete()
sys.exit()
def popup_dialog(parent):
dia = Widgets.Dialog(title="Dialog Title",
buttons=[('ok', 0), ('cancel', 1)],
parent=parent, modal=True)
cntr = dia.get_content_area()
cntr.add_widget(Widgets.Label("My Dialog Content"))
dia.show()
logger = log.get_logger('test', log_stderr=True, level=20)
app = Widgets.Application(logger=logger)
if hasattr(app, 'script_imports'):
app.script_imports.append('jqx')
app.add_callback('shutdown', quit)
top = app.make_window("Ginga Wrapped Widgets Example: %s" % (wname))
top.add_callback('close', quit)
vbox = Widgets.VBox()
vbox.set_border_width(2)
vbox.set_spacing(1)
dia = None
if wname == 'label':
w = Widgets.Label("Hello World label")
vbox.add_widget(w, stretch=1)
elif wname == 'button':
w = Widgets.Button("Press me")
w.add_callback('activated', lambda w: logger.info("button was clicked"))
w.add_callback('activated', lambda w: popup_dialog(top))
vbox.add_widget(w, stretch=1)
elif wname == 'textentry':
w = Widgets.TextEntry()
w.set_text("Hello, World!")
vbox.add_widget(w, stretch=1)
elif wname == 'textentryset':
w = Widgets.TextEntrySet()
w.set_text("Hello, World!")
vbox.add_widget(w, stretch=1)
elif wname == 'textarea':
w = Widgets.TextArea(editable=True)
w.set_text("Hello, World!")
vbox.add_widget(w, stretch=1)
elif wname == 'checkbox':
w = Widgets.CheckBox("Check me")
vbox.add_widget(w, stretch=1)
elif wname == 'togglebutton':
w = Widgets.ToggleButton("Toggle me")
vbox.add_widget(w, stretch=1)
elif wname == 'radiobutton':
w = Widgets.RadioButton("Option 1")
vbox.add_widget(w)
vbox.add_widget(Widgets.RadioButton("Option 2", group=w))
vbox.add_widget(Widgets.RadioButton("Option 3", group=w))
elif wname == 'combobox':
w = Widgets.ComboBox()
for name in ["Larry", "Curly", "Moe"]:
w.append_text(name)
vbox.add_widget(w)
elif wname == 'spinbox':
w = Widgets.SpinBox(dtype=int)
w.set_limits(-10, 10, incr_value=1)
w.set_value(4)
vbox.add_widget(w)
elif wname == 'slider':
w = Widgets.Slider(orientation='horizontal')
w.set_limits(-10, 10, incr_value=1)
w.set_value(4)
vbox.add_widget(w)
elif wname == 'scrollbar':
w = Widgets.ScrollBar(orientation='horizontal')
w.add_callback('activated', lambda w, val: logger.info("value is %d" % val))
vbox.add_widget(w)
elif wname == 'progressbar':
w = Widgets.ProgressBar()
w.set_value(0.6)
vbox.add_widget(w)
elif wname == 'statusbar':
w = Widgets.StatusBar()
w.set_message("Hello, World! is my status")
vbox.add_widget(w)
elif wname == 'image':
w = Widgets.Image()
w.load_file(os.path.join(icondir, 'ginga-512x512.png'))
vbox.add_widget(w)
elif wname == 'treeview':
w = Widgets.TreeView(selection='single', sortable=True,
use_alt_row_color=True)
columns = [("Meal", 'meal'), ("Critic 1", 'review1'),
("Critic 2", 'review2'), ("Critic 3", 'review3')]
w.setup_table(columns, 1, 'meal')
tree = dict(Breakfast=dict(meal='Breakfast', review1="Delish!",
review2="Ugh!", review3="Meh"),
Lunch=dict(meal='Lunch', review1="Gross!",
review2="Interesting...", review3="Meh"),
Supper=dict(meal='Supper', review1="Meh",
review2="Meh", review3="Jolly good!"))
w.set_tree(tree)
vbox.add_widget(w, stretch=1)
elif wname == 'webview':
w = Widgets.WebView()
w.load_url("http://www.google.com/")
vbox.add_widget(w)
elif wname == 'frame':
w = Widgets.Frame(title="Frame Title")
w.set_widget(Widgets.Label("Framed content"))
vbox.add_widget(w)
elif wname == 'expander':
w = Widgets.Expander(title="Expander Title")
w.set_widget(Widgets.Label("Expander content"))
vbox.add_widget(w)
elif wname == 'hbox':
w = Widgets.HBox()
w.add_widget(Widgets.Label("Item 1"), stretch=0)
w.add_widget(Widgets.Label("Item 2"), stretch=1)
vbox.add_widget(w)
elif wname == 'vbox':
w = Widgets.VBox()
w.add_widget(Widgets.Label("Item 1"), stretch=0)
w.add_widget(Widgets.Label("Item 2"), stretch=1)
vbox.add_widget(w)
elif wname == 'splitter':
w = Widgets.Splitter(orientation='horizontal')
w.add_widget(Widgets.Label('Content of Pane 1'))
w.add_widget(Widgets.Label('Content of Pane 2'))
vbox.add_widget(w, stretch=1)
elif wname == 'scrollarea':
w = Widgets.ScrollArea()
img = Widgets.Image()
img.load_file(os.path.join(icondir, 'ginga-512x512.png'))
w.set_widget(img)
vbox.add_widget(w, stretch=1)
elif wname == 'tabwidget':
w = Widgets.TabWidget()
w.add_widget(Widgets.Label('Content of Tab 1'), title='Tab 1')
w.add_widget(Widgets.Label('Content of Tab 2'), title='Tab 2')
hbox = Widgets.HBox()
sbox = Widgets.SpinBox(dtype=int)
sbox.set_limits(0, 1, incr_value=1)
sbox.set_value(0)
sbox.add_callback('value-changed', lambda sbx, val: w.set_index(val))
hbox.add_widget(sbox)
vbox.add_widget(w, stretch=1)
vbox.add_widget(hbox, stretch=0)
elif wname == 'stackwidget':
w = Widgets.StackWidget()
w.add_widget(Widgets.Label('Content of Stack 1'))
w.add_widget(Widgets.Label('Content of Stack 2'))
vbox.add_widget(w, stretch=1)
elif wname == 'mdiwidget':
w = Widgets.MDIWidget()
w.add_widget(Widgets.Label('Content of MDI Area 1'))
w.add_widget(Widgets.Label('Content of MDI Area 2'))
vbox.add_widget(w, stretch=1)
elif wname == 'gridbox':
w = Widgets.GridBox(rows=2, columns=2)
w.add_widget(Widgets.Label('Content of Grid Area 1'), 0, 0)
w.add_widget(Widgets.Label('Content of Grid Area 2'), 0, 1)
w.add_widget(Widgets.Label('Content of Grid Area 3'), 1, 0)
w.add_widget(Widgets.Label('Content of Grid Area 4'), 1, 1)
vbox.add_widget(w, stretch=1)
elif wname == 'menubar':
w = Widgets.Menubar()
menu = w.add_name('Menu 1')
menu.add_name('Larry').add_callback('activated',
lambda *args: print("chose Larry"))
menu.add_name('Curly').add_callback('activated',
lambda *args: logger.info("chose Curly"))
menu.add_name('Moe').add_callback('activated',
lambda *args: logger.info("chose Moe"))
vbox.add_widget(w)
vbox.add_widget(Widgets.Label("App content"), stretch=1)
elif wname == 'toolbar':
w = Widgets.Toolbar()
menu = w.add_menu('Menu Type 1', mtype='tool')
menu.add_name('Larry').add_callback('activated',
lambda w: logger.info("chose Larry"))
menu.add_name('Curly').add_callback('activated',
lambda w: logger.info("chose Curly"))
menu.add_name('Moe').add_callback('activated',
lambda w: logger.info("chose Moe"))
menu = w.add_menu('Menu Type 2', mtype='mbar')
menu.add_name('Frank')
menu.add_name('Dean')
menu.add_name('Sammy')
w.add_widget(Widgets.Button('A Button'))
w.add_separator()
w.add_action("Toggle me", toggle=True)
w.add_action(None, iconpath=os.path.join(icondir, 'hand_48.png'))
vbox.add_widget(w)
vbox.add_widget(Widgets.Label("App content"), stretch=1)
elif wname == 'dialog':
dia = Widgets.Dialog(title="Dialog Title",
buttons=[('ok', 0), ('cancel', 1)],
parent=top, modal=False)
dia.add_callback('activated',
lambda w, rsp: logger.info("user chose %s" % (rsp)))
cntr = dia.get_content_area()
cntr.add_widget(Widgets.Label("My Dialog Content"))
# add some content to main app widget
w = Widgets.Label("Hello World label")
vbox.add_widget(w, stretch=1)
hbox = Widgets.HBox()
w = Widgets.Button("Open Dialog")
w.add_callback('activated', lambda w: dia.show())
hbox.add_widget(w)
w = Widgets.Button("Close Dialog")
w.add_callback('activated', lambda w: dia.hide())
hbox.add_widget(w)
vbox.add_widget(hbox)
else:
# default to label
logger.error("Don't understand kind of widget '%s'" % (wname))
w = Widgets.Label("Hello World label")
vbox.add_widget(w, stretch=1)
top.set_widget(vbox)
top.show()
top.raise_()
if dia is not None:
dia.show()
try:
app.mainloop()
except KeyboardInterrupt:
print("Terminating viewer...")
if top is not None:
top.close()