要先裝pyhton-gtk2 python-gtk2-doc
作一個 WINDOW UI
import gtk
window = gtk.Window()
window.connect("destroy", gtk.main_quit)
button = gtk.Button("Hello PyGTK - BotBook.com")
window.add(button)
window.show_all()
gtk.main()
開啟 DATA 目錄底下的圖片檔案IMAGE.JPG
import gtk, os
window = gtk.Window()
window.connect("destroy", gtk.main_quit)
image=gtk.Image()
window.add(image)
image.set_from_file(os.path.join("data", "image.jpg"))
window.show_all()
gtk.main()
開啟DATA目錄底下多個檔案(PAGE-UP/PAGE-DOWN)
import gtk, os
dir="data"
pixbufs=[]
image=None
pos=0
def loadImages():
for file in os.listdir(dir):
filePath=os.path.join(dir, file)
pix=gtk.gdk.pixbuf_new_from_file(filePath)
pixbufs.append(pix)
print("Loaded image "+filePath)
def keyEvent(widget, event):
global pos
key = gtk.gdk.keyval_name(event.keyval)
if key=="space" or key=="Page_Down":
pos+=1
image.set_from_pixbuf(pixbufs[pos])
elif key=="b" or key=="Page_Up":
pos-=1
image.set_from_pixbuf(pixbufs[pos])
else:
print("Key "+key+" was pressed")
def main():
global image
window = gtk.Window()
window.connect("destroy", gtk.main_quit)
window.connect("key-press-event", keyEvent)
image=gtk.Image()
window.add(image)
loadImages()
image.set_from_pixbuf(pixbufs[pos])
window.show_all()
gtk.main()
if __name__=="__main__":
main()
圖片檔案全螢幕
#!/usr/bin/env python
# fullScreenScale.py - Show image scaled to full screen
# (c) Kimmo Karvinen & Tero Karvinen http://BotBook.com
import gtk, os
def fitRect(thing, box):
# scale
scaleX=float(box.width)/thing.width
scaleY=float(box.height)/thing.height
scale=min(scaleY, scaleX)
thing.width=scale*thing.width
thing.height=scale*thing.height
# center
thing.x=box.width/2-thing.width/2
thing.y=box.height/2-thing.height/2
return thing
def scaleToBg(pix, bg):
fit=fitRect(
gtk.gdk.Rectangle(0,0, pix.get_width(), pix.get_height()),
gtk.gdk.Rectangle(0,0, bg.get_width(), bg.get_height())
)
scaled=pix.scale_simple(fit.width, fit.height, gtk.gdk.INTERP_BILINEAR)
ret=bg.copy()
scaled.copy_area(
src_x=0, src_y=0,
width=fit.width, height=fit.height,
dest_pixbuf=ret,
dest_x=fit.x, dest_y=fit.y
)
return ret
def newPix(width, height, color=0x000000ff):
pix=gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, width , height)
pix.fill(color)
return pix
def main(): bs
pix=gtk.gdk.pixbuf_new_from_file(os.path.join("data/image1.jpg"))
window = gtk.Window()
window.connect("destroy", gtk.main_quit)
window.fullscreen()
bg=newPix(gtk.gdk.screen_width(), gtk.gdk.screen_height())
pixFitted=scaleToBg(pix, bg)
image=gtk.image_new_from_pixbuf(pixFitted)
window.add(image)
window.show_all()
gtk.main()
if __name__ == "__main__":
main()