Manipulando imagens em pyGTK
Exemplo bem simples de como substituir uma imagem em uma área de desenho.
Importante: Substituir os elementos de self.images em teste.py por imagens do filesystem local.
Arquivo teste.glade
<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*--> <!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd"> <glade-interface> <widget class="GtkWindow" id="mainwindow"> <property name="visible">True</property> <property name="events">GDK_KEY_PRESS_MASK</property> <property name="title" translatable="yes">window1</property> <property name="type">GTK_WINDOW_TOPLEVEL</property> <property name="window_position">GTK_WIN_POS_NONE</property> <property name="modal">False</property> <property name="resizable">True</property> <property name="destroy_with_parent">False</property> <property name="decorated">True</property> <property name="skip_taskbar_hint">False</property> <property name="skip_pager_hint">False</property> <property name="type_hint">GDK_WINDOW_TYPE_HINT_NORMAL</property> <property name="gravity">GDK_GRAVITY_NORTH_WEST</property> <property name="focus_on_map">True</property> <signal name="delete_event" handler="on_mainwindow_delete_event" last_modification_time="Fri, 11 Nov 2005 10:42:50 GMT"/> <child> <widget class="GtkDrawingArea" id="drawingarea"> <property name="visible">True</property> </widget> </child> </widget> </glade-interface>
Arquivo teste.py
1 import gtk, gtk.glade, random
2
3 class MainWindow(object):
4
5 def __init__(self,gladefile):
6
7 # The main window widget tree
8 self.wTree = gtk.glade.XML(gladefile, "mainwindow")
9
10 # The main window
11 self.mainwindow = self.wTree.get_widget("mainwindow")
12
13 # The Drawing Area Widget
14 self.draw = self.wTree.get_widget("drawingarea")
15
16 self.images = ['img1.jpg','img2.jpg','img3.jpg','img4.jpg','img5.jpg']
17
18 # Signal and Event handlers
19 self.signals_and_events()
20
21
22 def signals_and_events(self):
23 HANDLERS_AND_METHODS = {
24 "on_mainwindow_delete_event": self.destroy,
25 }
26
27 # Event handling
28 self.mainwindow.connect("key_press_event",self.paint)
29 self.draw.connect("expose_event",self.paint)
30
31 # Signal handling
32 self.wTree.signal_autoconnect(HANDLERS_AND_METHODS)
33
34
35 def paint(self,widget,event):
36 areaw, areah = self.draw.window.get_size()
37
38 style = self.draw.get_style()
39 gc = style.fg_gc[gtk.STATE_NORMAL]
40
41 # Get the image
42 img = self.images[int(random.random() * 10) % len(self.images)]
43 currpix = gtk.gdk.pixbuf_new_from_file(img)
44
45 # Get the image buffer dimensions
46 pw = currpix.get_width()
47 ph = currpix.get_height()
48
49 # Resize the drawing area
50 self.draw.window.resize(pw,ph)
51
52 # Scroll the window, if necessary
53 self.draw.set_size_request(pw,ph)
54
55 # Draw the pixbuf into the drawing area
56 self.draw.window.draw_pixbuf(gc, currpix, 0, 0, 0, 0, pw,ph)
57
58
59 def destroy(self, widget, event=None):
60 gtk.main_quit()
61
62
63 if __name__ == "__main__":
64 app = MainWindow("teste.glade")
65 gtk.main()