In this part of the wxPython tutorial, we will create some simple examples.
We start with a very simple example. Our first script will only show a small window. It will not do much. We will analyse the script line by line.
A wx.Frame widget is an important container widget. We will analyse this widget in detail later. The wx.Frame widget is a parent widget for other widgets. It has no parent itself. If we specify None for a parent parameter we indicate that our widget has no parents. It is a top widget in the hierarchy of widgets. After we create the wx.Frame widget, we must call the Show() method to actually display it on the screen.
The last line enters the mainloop. The mainloop is an endless cycle. It catches and dispatches all events that exist during the life of our application.
This was a very simplistic example. Despite this simplicity we can do quite a lot with this window. We can resize the window, maximise it, minimise it. This functionality requires a lot of coding. All this is hidden and provided by default by the wxPython toolkit. There is no reason for reinventing the wheel.
wx.Frame widget is one of the most important widgets in wxPython. It is a container widget. It means that it can contain other widgets. Actually it can contain any window that is not a frame or dialog. wx.Frame consists of a title bar, borders and a central container area. The title bar and borders are optional. They can be removed by various flags.
wx.Frame has the following constructor:
Our intention was to display a window without a minimise box. So we did not specify this flag in the style parameter.
Size and Position
We can specify the size of our application in two ways. We have a size parameter in the constructor of our widget or we can call the SetSize() method:
Similarly, we can position our application on the screen. By default the window is placed in the upper left corner of the screen. But it can differ on various OS platforms or even window managers. Some window managers place application windows themselves. Some of them do some optimisation, so that windows do not overlap. A programmer can position the window programmatically. We already saw a pos parameter in the constructor of our wx.Frame widget. By providing other than the default values, we can control the position ourselves:
There are several methods to do this.
Centering on the screen
If we want to center our application on the screen, wxPython has a handy method. The Centre() method simply centers the window on the screen. No need to calculate the width and the height of the screen. Simply call the method:
* Next - wxPython - Menus and toolbars
* 八款 Python GUI 開發框架
* WxPython Application Development Cookbook
* tutorialspoint - wxPython - Frame Class