Welcome folks today in this blog post we will be building a image viewer
in wxpython and displaying bitmap/png/jpg
images inside frames panels using frame and image
class. All the full source code of the application is given below.
Get Started
In order to get started you need to install the below library using the pip
command as shown below
pip install wxpython
After installing this library you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#!/usr/bin/env python """Hello, wxPython! program.""" #wxPython is a cross-platform GUI toolkit for the Python programming language import wx class Frame(wx.Frame): """Frame class that displays an image.""" def __init__(self, image, parent=None, id=-1, pos=wx.DefaultPosition, title='Hello, wxPython!'): """Create a Frame instance and display image.""" temp = image.ConvertToBitmap() size = temp.GetWidth(), temp.GetHeight() wx.Frame.__init__(self, parent, id, title, pos, size) self.bmp = wx.StaticBitmap(parent=self, bitmap=temp) self.SetClientSize(size) class App(wx.App): """Application class.""" def OnInit(self): image = wx.Image('wxPython.jpg', wx.BITMAP_TYPE_JPEG) self.frame = Frame(image) self.frame.Show() self.SetTopWindow(self.frame) return True def main(): app = App() app.MainLoop() if __name__ == '__main__': main() |
So as you can see in the above figure we are using the frames and images
class and passing the path
of the image and then showing it inside the image viewer
as shown below
If you execute the above python script
by typing the below command
python app.py