For a project I needed to display an image in a GUI. As I have used wxPython in the past, I choose to use it again. Although the toolkit is extremely simple and easy to use, I still needed some time to write this simple image viewer. The problem was, that I expected to be able to attach a wx.Image to a Sizer. It took me sometime to figure out, that I needed to turn it into a wx.StaticBitmap first.
Here is a very small and simple image viewer with wxPython, may it be of help to the internet ;)
#!/usr/bin/env python import wx class FooApp(wx.App): # called when the 'change' button is pressed def changeImage(self,event): img = wx.Image("foo.jpg") self.image = wx.StaticBitmap(self.panel, wx.ID_ANY, wx.BitmapFromImage(img)) def __init__(self): # setup code for the window wx.App.__init__(self) self.frame = wx.Frame(None, title='Demo') self.panel = wx.Panel(self.frame) # load an image img = wx.Image("bla.jpg") self.image = wx.StaticBitmap(self.panel, wx.ID_ANY, wx.BitmapFromImage(img)) # create a Sizer to hold one (or more) button self.buttons = wx.BoxSizer(wx.VERTICAL) self.changeButton = wx.Button(self.panel, -1, "Change") self.changeButton.Bind(wx.EVT_BUTTON,self.changeImage) self.buttons.Add(self.changeButton) self.mainSizer = wx.BoxSizer(wx.VERTICAL) self.mainSizer.Add(self.image) self.mainSizer.Add(self.buttons) # more generic setupcode self.panel.SetSizer(self.mainSizer) self.mainSizer.Fit(self.frame) self.panel.Layout() self.frame.Show(True) if __name__ == '__main__': app = FooApp() app.MainLoop()
Just for the record: here are the websites that helped me write this code: