2005年4月30日

GtkDrawingArea and GtkScrolledWindow


I saw some question asked about making flashget like progress window in gtk+. It can be simply realized using DrawingArea.



Then there is also the problem of scrolling the DrawingArea inside a ScrolledWindow. Well, this is simple. Just put a Viewport b/w the DrawingArea and the ScrolledWindow. Then do a set_size_request on the DrawingArea.





#!/usr/bin/python
import pygtk
pygtk.require('2.0')
import gtk

# 2 icons we use in our drawing area.
PICTNAME1="gdraw1.png"
PICTNAME2="gdraw2.png"
def load(pix):
'''Load GUI'''
win = gtk.Window()
gdraw = gtk.DrawingArea()
sw = gtk.ScrolledWindow()
sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
vp = gtk.Viewport()
sw.add(vp)
vp.add(gdraw)

win.add(sw)
win.resize(400, 200)
win.connect('delete-event', gtk.main_quit)
gdraw.connect('expose-event', expose_pix, pix)
win.show_all()

def expose_pix(gdraw, event, pix):
'''At expose event, actual draw graph.'''
pix1, pix2 = pix
gc = gdraw.window.new_gc()
pw = pix1.get_width()
ph = pix1.get_height()
geo = gdraw.window.get_geometry()
gw, gh = geo[2:4]
dx = dy = 0

# Draw 100 pixbuf on DrawingArea
for i in range(100):
if dx + pw > gw:
# Move down to the next line
dx = 0
dy += ph+1
if i % 7: # Draw a pix2 every 7 pix1
pixi = pix1
else:
pixi = pix2
gdraw.window.draw_pixbuf(gc, pixi, 0, 0,
dx, dy, pw, ph)
dx += pw+1
# Set the gdraw size request for Viewport
# to the height of our drawing.
gdraw.set_size_request(-1, dy+ph)

def main():
pix1 = gtk.gdk.pixbuf_new_from_file(PICTNAME1)
pix2 = gtk.gdk.pixbuf_new_from_file(PICTNAME2)
load((pix1, pix2))
gtk.main()

if __name__ == '__main__':
main()




Told you pygtk is very easy to program with.

没有评论: