15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

15 'Animals (in pairs; min: pair, max: dozen)')<br />

16 box.pack_start(lb)<br />

17<br />

18 sb = gtk.HBox(False, 0)<br />

19 adj = gtk.Adjustment(2, 2, 12, 2, 4, 0)<br />

20 sl = gtk.Label('Number:')<br />

21 sl.modify_font(<br />

22 pango.FontDescription("Arial Bold 10"))<br />

23 sb.pack_start(sl)<br />

24 ct = gtk.SpinButton(adj, 0, 0)<br />

25 sb.pack_start(ct)<br />

26 box.pack_start(sb)<br />

27<br />

28 cb = gtk.HBox(False, 0)<br />

29 c2 = gtk.Label('Type:')<br />

30 cb.pack_start(c2)<br />

31 ce = gtk.combo_box_entry_new_text()<br />

32 for animal in ('dog', 'cat','hamster', 'python'):<br />

33 ce.append_text(animal)<br />

34 cb.pack_start(ce)<br />

35 box.pack_start(cb)<br />

36<br />

37 qb = gtk.Button("")<br />

38 red = gtk.gdk.color_parse('red')<br />

39 sty = qb.get_style()<br />

40 for st in (gtk.STATE_NORMAL,<br />

41 gtk.STATE_PRELIGHT, gtk.STATE_ACTIVE):<br />

42 sty.bg[st] = red<br />

43 qb.set_style(sty)<br />

44 ql = qb.child<br />

45 ql.set_markup('QUIT')<br />

46 qb.connect_object("clicked",<br />

47 gtk.Widget.destroy, top)<br />

48 box.pack_start(qb)<br />

49 top.add(box)<br />

50 top.show_all()<br />

51<br />

52 if __name__ == '__main__':<br />

53 animal = GTKapp()<br />

54 gtk.main()<br />

Line-by-Line Explanation<br />

Lines 16<br />

We import three different modules and packages, PyGTK, GTK, and Pango, a library for layout and<br />

rendering of text, specifically for I18N purposes. We need it here because it represents the core of text<br />

and font handling for GTK+ (2.x).<br />

Lines 851<br />

The GTKapp class represents all the widgets of our application. The topmost window is created (with<br />

handlers for closing it via the window manager), and a vertically oriented sizer (VBox) is created to hold

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!