How to use Frame in wxpython for Python instances

Source: Internet
Author: User

This section describes how to use wxpython Frame.

Example:

Copy codeThe Code is as follows:
#! /Usr/bin/python
#-*-Coding: GBK -*-
# Simple. py

Import wx

App = wx. App ()
Frame = wx. Frame (None)
Frame. Show ()
App. MainLoop ()

Example 2,

Copy codeThe Code is as follows:
#! /Usr/bin/python
Tell the program the path of the python interpreter, which is only useful in linux. Writing this line of code in Windows does not work, but it is okay.

#-*-Coding: GBK -*-
Tell the python system character encoding so that we can use Chinese characters in the program. If this line does not exist, it is garbled. Windows is GBK, Linux is UTF-8

App = wx. App ()
Initialize the application

Frame = wx. Frame (None)
Frame. Show ()
Create a Frame (form) and display it.

App. MainLoop ()
The application enters the message loop

Frame is actually a form. When a form is generated, no parameters are provided. Only a default form is generated and the code of the default form is generated:
Frame = wx. Frame (None)

In fact, wx. Frame can pass in many parameters to control the style and behavior of the generated form. Now let's look at the prototype of the Frame constructor:
_ Init _ (self, Window parent, int id =-1, String title = EmptyString,
Point pos = DefaultPosition, Size size = DefaultSize,
Long style = DEFAULT_FRAME_STYLE, String name = FrameNameStr)

Parameter 1: parent
Parent window of the current window. If the current window is top-level window, parent = None. If it is not a top-level window, its value is the name of the frame to which it belongs.

Parameter 2: id
Form Number. If it is set to-1, the system automatically assigns a number to it. The default value is-1.

Parameter 3: title
The title bar of the form, that is, Caption. The default value is null.

Parameter 4: pos
Coordinates of the form. The default value is (-1,-1), and the position of the form is determined by the system.

Parameter 5: size
The size of the form. The default value is (-1,-1). The size of the form is determined by the system.

Parameter 6: style
Form style. The default value is DEFAULT_FRAME_STYLE.

The default style DEFAULT_FRAME_STYLE is the combination of the following values:
Wx. MINIMIZE_BOX | wx. MAXIMIZE_BOX | wx. RESIZE_BORDER |
Wx. SYSTEM_MENU | wx. CAPTION | wx. CLOSE_BOX | wx. CLIP_CHILDREN

It includes the Minimize button, maximize button, system menu, title bar, close button, variable size, and so on. You can also change the style as needed. For more information, see help.

Parameter 7: name
Form name.

We can see that six of the seven parameters have default values, and only the first parameter parent needs to be set. Therefore, the simplest form is:

Add Python code to favorites
Frame = wx. Frame (None)

Form title:

Add Python code to favorites
Frame = wx. Frame (None, title = "Hello World ")

Location and size:

Add Python code to favorites
Frame = wx. Frame (None, title = "Hello World", size = (300,150), pos = (200,200 ))

Style 1: Only one bald title bar

Add Python code to favorites
Frame = wx. Frame (None, style = wx. CAPTION)

Style 2: only the customer zone, no title bar, and no size change allowed

Add Python code to favorites
Frame = wx. Frame (None, style = 0)

Style 3: only one close button cannot be changed

Copy codeThe Code is as follows:
Frame = wx. Frame (None, style = wx. SYSTEM_MENU | wx. CAPTION | wx. CLOSE_BOX)
Wx. DEFAULT_FRAME_STYLE: This is the default style of each window, including the title, adjustable border, Max-Minimum button, close button, and system menu.
Wx. CAPTION: Add a title bar on the framework to display the title attributes of the framework.
Wx. CLOSE_BOX: indicates that the system displays a closed box on the frame title bar, using the default system position and style.
Wx. FRAME_ON_TOP: Top Window
W x. FRAME_SHAP ED: the framework created with this style can use the SetShape () method to create a non-rectangular window.
Wx. FRAME_TOOL_WINDOW: a smaller title bar than the normal one to make the framework look like a Toolbox window. In Windows, the framework created using this style does not appear on the taskbar that displays all opened Windows.
Wx. MAXIMIZE_BOX: indicates that the system displays a maximize box on the frame title bar, using the default system position and style.
Wx. MINIMIZE_BOX: indicates that the system displays a minimal box on the frame title bar, using the default system position and style.
Wx. RESIZE_BORDER: adds a border to the frame that can be changed.
Wx. SIMPLE_BORDER: no decorative border. Cannot work on all platforms.
Wx. SYSTEM_MENU: adds the system menu (with functions such as closing, moving, and changing the size) and closes the box to this window. The effect of changing the size and disabling functions in the System Menu depends on whether the wx. MAXIMIZE_BOX, wx. MINIMIZE_BOX, and wx. CLOSE_BOX styles are applied.
Wx. FRAME_EX_META: if it is in MacOS, this attribute is used to show "metal style"
Wx. FRAME_EX_CONTEXTHELP: Indicates whether a connection HELP button exists.
Wx. FRAME_FLOAT_ON_PARENT: whether the window is displayed on the top. Unlike wxSTAY_ON_TOP, the window must have a parent window.

The frame form style is set by the style attribute, for example:
Style = wx. SYSTEM_MENU | wx. MINIMIZE_BOX | wx. CLOSE_BOX | wx. CAPTION

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.