This article mainly introduces the wxPython application example of python. taking loading images as an example, it describes the usage of wxPython, which is of great practical value, for more information about how to use python wxPython, see the examples in this article. The specific method is as follows:
Let's take a look at the effect. here we load an image:
The code is as follows:
#!/usr/bin/env python """hello wxPython program""" import wx class Frame(wx.Frame): #wxPrame subclass """Frame class that display a image""" def __init__(self, image, parent=None, id=-1, pos=wx.DefaultPosition, title="Hello ,wxPython"): #3 #create a Frame instance and display a 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) pass class App(wx.App): #5 wx.App subclass """Application class""" def OnInit(self): image = wx.Image('wxPython.jpg',wx.BITMAP_TYPE_JPEG) self.frame = Frame(image) self.frame.Show(True) self.SetTopWindow(self.frame) return True def main(): app = App() app.MainLoop() if __name__ == "__main__": main()
To sum up the four steps for using wxPython:
1. import the wxPython package
2. subclass application class
3. define the initialization method of an application
4. create an instance of the application class (subclass created in step 1)
5. go to the main event loop of the application-class instance created in step 1.
Note: wxPython.jpg is an image of a. Gump, which must be in the same directory as the program.
I hope this article will help you with Python programming.