~ubuntu-branches/ubuntu/trusty/python-enable/trusty

« back to all changes in this revision

Viewing changes to examples/kiva/wx_image_viewer.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-04-05 21:54:28 UTC
  • mfrom: (1.1.5 upstream)
  • mto: (8.2.1 sid)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: james.westby@ubuntu.com-20110405215428-1x2wtubz3ok2kxaq
Tags: upstream-3.4.1
ImportĀ upstreamĀ versionĀ 3.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import wx
2
 
import sys
3
 
import os
4
 
from enthought.kiva import Canvas
5
 
from enthought.kiva.agg import GraphicsContextArray
6
 
from enthought.kiva.backend_image import Image
7
 
 
8
 
#Event ID numbers
9
 
ID_OPEN = 101
10
 
ID_QUIT = 102
11
 
 
12
 
class ImageCanvas(Canvas):
13
 
    def __init__(self, parent, id=-1, size=wx.DefaultSize):
14
 
        Canvas.__init__(self, parent, id, size=size)
15
 
 
16
 
        # Until we have an image, use a blank GraphicsContextArray
17
 
        self.img = GraphicsContextArray((800, 600))
18
 
        return
19
 
 
20
 
    def load_image(self, filename):
21
 
        # Create a new Image object for the new file and then make wx repaint the window
22
 
        self.img = Image(filename)
23
 
        self.dirty = 1
24
 
        self.Refresh()
25
 
    
26
 
    def do_draw(self, gc):
27
 
        # Use Image's abillity to draw itself onto a gc to paint the window
28
 
        gc.draw_image(self.img, (0,0,self.img.width(), self.img.height()))
29
 
 
30
 
class ImageViewerWindow(wx.Frame):
31
 
    def __init__(self, id=-1, title="Image Viewer", size=(800,600)):
32
 
        parent = None
33
 
        wx.Frame.__init__(self, parent, id, title, size=size)
34
 
 
35
 
        # Setup the menubar and file menu
36
 
        filemenu = wx.Menu()
37
 
        filemenu.Append(ID_OPEN, "&Open", "Open an image file to view")
38
 
        filemenu.Append(ID_QUIT, "&Quit", "Quit the program")
39
 
        menubar = wx.MenuBar()
40
 
        menubar.Append(filemenu, "&File")
41
 
        self.SetMenuBar(menubar)
42
 
 
43
 
        # Register the event IDs for each menu item to call the appropriate function
44
 
        wx.EVT_MENU(self, ID_QUIT, self.onQuit)
45
 
        wx.EVT_MENU(self, ID_OPEN, self.onOpen)
46
 
        
47
 
        #Create an ImageCanvas to hold the image
48
 
        self.canvas = ImageCanvas(self)
49
 
 
50
 
        self.Show(1)
51
 
        return
52
 
    
53
 
 
54
 
    def onQuit(self, e):
55
 
        sys.exit()
56
 
        
57
 
    def onOpen(self, e):
58
 
        #Create a file dialog
59
 
        dlg = wx.FileDialog(
60
 
            self, message = "Choose an image", defaultDir = os.getcwd(),
61
 
            defaultFile = "", wildcard = "All Files (*.*)|*.*",
62
 
            style=wx.OPEN | wx.CHANGE_DIR
63
 
            )
64
 
        #Show it modally.  If the user clicks OK, have the ImageCanvas load the
65
 
        # new file.
66
 
        if dlg.ShowModal() ==wx.ID_OK:
67
 
            paths = dlg.GetPaths()
68
 
            self.canvas.load_image(paths[0])
69
 
        
70
 
 
71
 
if __name__ == "__main__":
72
 
    class MyApp(wx.App):
73
 
        def OnInit(self):
74
 
            ImageViewerWindow(size=(800,600))
75
 
            return 1
76
 
    
77
 
    app = MyApp(0)
78
 
    app.MainLoop()