Wxpython Development Summary---Frame,panel and a project framework __python

Source: Internet
Author: User

Some time ago in Python developed an educational class of PC desktop applications, while learning and development, now one after another in the development process to learn something to share the record, the project uses the Wxpython framework.



Wxpython's official website : https://www.wxpython.org/

WxPython The official manual of various control usages : http://xoomer.virgilio.it/infinity77/wxPython/widgets.html


WX Library is not Python's own library files, you need to first download and install in the official website, windows can download EXE installation files at the official website, Ubuntu and Mac can use pip install to install, can also use the source installation method installation.

After the installation is successful, using import WX to test the success of the installation, in Wxpython programming, you also need to import the Wxpython Library reference using import WX.

Here is the first interface window built with Wxpython:

"Effect"


"Code Implementation"

#coding =utf-8 "" "" "ImportWxclassFirseframe (WX. Frame):def__init__ (self, parent=none, id=-1, title= ', pos=wx. Defaultposition, Size=wx. DefaultSize, style= WX. Default_frame_style): "" "WX. Frame () constructor parameter description: WX. Frame.__init__ (self, Window parent, int id=-1, String title=emptystring, point pos=defaultposition, Size size= DefaultSize, Long Style=default_frame_style, String name=framenamestr)-> FRAME "" WX. Frame.__init__ (self, parent, ID, title, pos, size, style) self. Initui () PassdefInitui (self): self. Setbackgroundcolour (' #ffffff ') "" WX. Statictext () constructor parameter description: __init__ (self, Window parent, int id=-1, String label=emptystring, point pos=de Faultposition, Size size=defaultsize, Long style=0, String name=statictextnamestr)-> statictext "" "CONTENT = wx." Statictext (self,-1, U ' This is the first window ', pos=) content. Setforegroundcolour (' #000000 ') Pass classMainapp (WX. APP):defOnInit (self): Self.frame = Firseframe (id=-1, title=u ' first window ', pos= (Ten), size= (230, 230)) Self.frame.Sho W () returnTruedefMain (): app = Mainapp () app. Mainloop ()if__name__ = = "__main__": Main ()


"description"

__name__ = = "__main__":
    Main ()
The code above is the main entry point for a Python program, and there is only one in a project where a Python program starts to run, and the portal is found.

Mainapp (WX. APP):
    OnInit (self):
        self.frame = Firseframe (id=-1, title=u ' first window ', pos= (Ten), size= (230, 230))
        self.frame.Show ()
         True

Wx. App is an App object in Wxpython, there is only one in a Wxpython, Mainapp class inherits the Wx.app class Mainapp class is instantiated, invokes Wx.app object Mainloop () Method begins a Wxpython process.


On the problem of coding errors, in the above program, the Chinese character string is preceded by a U, indicating that the string encoding/decoding method is Unicode, if the project runs a Unicode encoding error, you can add the following code at the beginning of the program:

SYS

Reload (SYS)
sys.setdefaultencoding (' Utf-8 ')

(1) wx. Frame

Frame can be viewed as a window drawn by the control provided by Wxpython, and we can draw the control on a frame for easy management.

In the Wxpython source, you can see that the Wxframe constructor is:

"" "
wx." Frame () constructor parameter description:
__init__ (self, Window parent, int id=-1, String title=emptystring, point
    pos=defaultposition , Size size=defaultsize,
   long Style=default_frame_style, String name=framenamestr)-> FRAME ""
"description"

The POS is the position where the frame is displayed on the screen, and the size for the frame window initializes the drawing

Style is the property that the frame displays, and the common style style description:

Style=wx. CAPTION: Add a title bar to the frame:



Style=wx. Close_box: Display the close box on the frame:

Style=wx. Maximize_box: Display the Maximize box on a frame

Style=wx. Minimize_box: Display the minimized box on a frame

Style=wx. Resize_border: Add a border to frame that a user can change the frame size by himself

Style=wx. Default_frame_style: Default FRAME style:


You can see that the default style includes all the common styles (including, title, icon, minimize, Maximize, drag-and-drop border, close), so if we want to go out a style based on the default style, we can:

style = WX. default_frame_style^ (WX. Resize_border|wx. Maximize_box|wx. Minimize_box|wx. Close_box)
The above style is based on the default style, removes drag-and-drop changes to the frame size property, removes the maximized, removes the minimize, and removes the closure.

The effect is as follows:


Of course, if you only remove one style, e.g. maximize removal:

style = WX. Default_frame_style^wx. Maximize_box
self.frame = Firseframe (id=-1, title=u ' first window ', pos= (Ten), size= (230, 230), Style=style)
Effect:



A POS is a frame that is displayed on the screen at the coordinates of the position, and size is the magnitude of the frame initialization, so if we want our frame to appear in the middle of the screen after initialization, we can first get the size of the screen and then dynamically specify the value of the POS:

Mainapp (WX. APP):
    OnInit (self):
        style = wx. Default_frame_style^wx. Maximize_box
        # WX. Displaysize () obtains a tuple, e.g. (1440, 780) represents the width of the current device screen
         windowsize = wx. Displaysize ()
        Newpos = (Windowsize[0]/2, windowsize[1]/2)
        Self.frame = Firseframe (id=-1, title=u ' first window ', pos= Newpos, size= (230, 230), Style=style)
        self.frame.Show ()
        True
The code above is passed by WX. Displaysize () obtained the size of the device screen, dynamically set the coordinates of the frame display, so that the upper left corner of the frame is located in the center of the screen, which is a method, in fact, Wxpython provides a self-contained interface:

Modify only the Wxframe class:

Firseframe (WX. Frame):
    __init__ (self, parent=none, id=-1, title= ', pos=wx. DefaultSize, Size=wx. DefaultSize, style= WX. Default_frame_style):

        wx. Frame.__init__ (self, parent, ID, title, pos, size, style)

        self. Initui ()
        
    pass Initui (self):
        self. Setbackgroundcolour (' #ffffff ') ""
        wx. Statictext () constructor parameter description:
        __init__ (self, Window parent, int id=-1, String label=emptystring, point
            pos= Defaultposition, Size size=defaultsize,
            long style=0, String name=statictextnamestr)-> statictext "" "
        content = wx. Statictext (self,-1, U ' This is the first window ', pos=)
        content. Setforegroundcolour (' #000000 ')

        self. Center ()
         Pass
In Firseframe (WX. Frame) at the end of the Initui () function called self. Center () function, which tells the system to display the frame in the middle of the screen, and the system places the center of the frame in the center of the device screen.

Similarly, if you want to maximize the frame after initialization, one way is to set the size of the frame to wx.displaysize (), and self. Center () is the same and can be invoked by calling self. Maximize (True) to implement:

Self. Maximize (True)

(2) wx. Panel

A panel can be viewed as a fragmented frame, where a subset of the controls need to be processed simultaneously, such as hiding simultaneously, and the parent window of these controls can be written in the same panel, which facilitates the overall operation.

"" "
__init__ (Self, window parent, int id=-1, point pos=defaultposition, 
    Size size=defaultsize, Long Style=wxtab _traversal|wxno_border, 
    String name=panelnamestr)-> Panel ""

(3) A WXPYTHON project structure

When we have multiple frame interfaces that require a jump back and forth, the frame is displayed in Mainapp (WX. APP) class, then you need to be in Mainapp (WX. APP) implements a frame jump interface through this interface to complete multiple frame jumps.


"description"

In Framemanager, a dictionary is maintained, Key=type,value=frame, when the frame is first displayed, the frame is created and cached in the dictionary, and the frame is displayed again directly from the dictionary to obtain the gaiframe. The data and display status of the frame is updated by means of the custom method Updatedataandui (self, NewData) interface in frame.


source code and more instructions please check:

A method of Wxpython to realize jump/update between frame

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.