Wxpython Study Notes (i)

Source: Internet
Author: User

Create the smallest empty Wxpython program
Import # 1 class APP (WX. APP):#2    def#3        frame = wx. FRAME (Parent=none, title='Bare')        frame. Show ()        return#4#5

Any line of the above code must not be small, otherwise it will not work. This basic WxPython program illustrates the five basic steps necessary to develop any WxPython program:

  1. Import the required WxPython Package
    Once you have imported the wx module, you will be able to create your application (application) objects andframe objects. Each WxPython program must have a application object and at least one frame object. The application object must be WX. an instance of the App or one of the subclasses that you defined in the OnInit () method . When your application starts, theOnInit () method will be WX. called by the App parent class.

  2. Subclasses of WxPython application class WX. App
    We usually create a frame object in the OnInit () method. The wx above. Frame accepts three parameters, only the first (Parent=none) is required, and the rest has default values. Call the Show () method to make the frame visible, otherwise it is not visible. We can set the visibility of a frame by giving show () a Boolean parameter

  3. Defining an application's initialization method
    We did not define a __init__ () method for our application class. In Python , this means that the parent method Wx.app.__init () __ will be called automatically when the object is created. It's a good thing. If you define your own __init__ () method, do not forget to call the __init () __ method of its base class, such as:
    class App (WX. APP):    def__init__(self):        Wx.app. __init__ (self)

    If you forget to do this,WxPython will not be initialized and your OnInit () method will not be called.

  4. To create an instance of an application class
  5. Enter the main event loop for this application
    Once in the main event loop, control is forwarded to WxPython. WxPython GUI programs primarily respond to user mouse and keyboard events. When all the frames of an application are closed, this app. The Mainloop () method returns and the program exits.
Extend this minimal empty Wxpython program
ImportWXclassFrame (WX. Frame):#3    PassclassApp (WX. APP):defOnInit (self): Self.frame= Frame (Parent=none, title='Spare')#4self.frame.Show () self.frame.Center () self. Settopwindow (Self.frame)#5        returnTrueif __name__=='__main__':#6App =app () app. Mainloop ()
    1. We changed the way the frame object was created. The bare version of the program simply creates a wx. an instance of the Frame class. In version spare , we define our own Frame class as wx. the subclass of the Frame. At this point, the final result is no different, but if you want to show things like text, buttons, and menus in your frame, you might want your own frame class.
    2. We will use a reference to the frame instance as a property of the application instance
    3. In the OnInit () method, we call the App class's own Settopwindow () method and pass it to our newly created frame instance. We do not have to define the Settopwindow () method because it inherits from WX. The APP parent class. The Settopwindow () method is an optional method that lets the WxPython method know which frame or dialog box will be considered primary . A WxPython program can have several frameworks, one of which is designed as the top-level window of an application.

    4. This is typically used in Python to test whether the module is run as a program or imported by another module. We do this by examining the __name__ property of the module.

Create the final hello.py program
ImportWXclassFrame (WX. Frame):#2 wx. Frame sub-class    """Frame class that displays an image."""    def __init__(Self, image, Parent=none, id=-1, POS=WX. Defaultposition, title='Hello, wxpython!.'):#3 Image Parameters        """Create a Frame instance and display image."""#4 displaying imagestemp = image. Converttobitmap ()#Convert to BitmapSize =temp. GetWidth (), temp. GetHeight () wx. Frame.__init__(self, parent, ID, title, pos, size) self.bmp= WX. Staticbitmap (Parent=self, bitmap=temp)classAPP (WX. APP):#5 WX. App Sub-category    """Application class."""    defOnInit (self):#6 Image ProcessingImage = Wx. Image ('back_image.jpg', WX. Bitmap_type_jpeg) Self.frame=Frame (image) Self.frame.Show () self. Settopwindow (Self.frame)returnTruedefMain ():#7App =app () app. Mainloop ()if __name__=='__main__': Main ()

#2 define a wx. the subclass of frame so that we can control the content and appearance of the frame more.

#3 Add an image parameter to the constructor of our framework. This value is provided by our application class when creating an instance of a framework. Similarly, we can pass the necessary values to WX. frame.__init__ ()

#4 We will use WX. Staticbitmap Control to display this image, it requires a bitmap. So we convert the image into a bitmap. We also use the width and height of the image to create a size tuple. This size tuple is provided to WX. Frame.__init__ () is called so that the dimensions of the frame match the bitmap dimensions.

#5 Define a WX with the OnInit () method . the subclass of the app, which is the basic requirement of the WxPython application.

#6 We created an image object using a file named wxpython.jpg in the same directory as hello.py .

#7 The main () function creates an instance of an application and starts the WxPython event loop.

Wxpython Study Notes (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.