Preface
In this part of the wxPython tutorial, we will create some simple examples.
Simple example
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.
- simply.py
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # simple.py
- import wx
- app = wx.App() # Create an application object.
- frame = wx.Frame(None, title='Simple application')
- frame.Show()
- app.MainLoop()
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
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:
- Frame(parent, id=ID_ANY, title="", pos=DefaultPosition,
- size=DefaultSize, style=DEFAULT_FRAME_STYLE, name=FrameNameStr)
- no_minimize.py
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # no_minimize.py
- import wx
- app = wx.App()
- frame = wx.Frame(None, style=wx.MAXIMIZE_BOX | wx.RESIZE_BORDER
- | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX)
- frame.Show(True)
- app.MainLoop()
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:
- set_size.py
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # set_size.py
- import wx
- class Example(wx.Frame):
- def __init__(self, parent, title):
- super(Example, self).__init__(parent, title=title,
- size=(350, 250))
- def main():
- app = wx.App()
- ex = Example(None, title='Sizing')
- ex.Show()
- app.MainLoop()
- if __name__ == '__main__':
- main()
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.
- moving.py
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # moving.py
- import wx
- class Example(wx.Frame):
- def __init__(self, parent, title):
- super(Example, self).__init__(parent, title=title,
- size=(300, 200))
- self.Move((800, 250))
- def main():
- app = wx.App()
- ex = Example(None, title='Moving')
- ex.Show()
- app.MainLoop()
- if __name__ == '__main__':
- main()
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:
- centering.py
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # centering.py
- import wx
- class Example(wx.Frame):
- def __init__(self, parent, title):
- super(Example, self).__init__(parent, title=title,
- size=(300, 200))
- self.Centre()
- def main():
- app = wx.App()
- ex = Example(None, title='Centering')
- ex.Show()
- app.MainLoop()
- if __name__ == '__main__':
- main()
Supplement
* Next - wxPython - Menus and toolbars
* 八款 Python GUI 開發框架
* WxPython Application Development Cookbook
* tutorialspoint - wxPython - Frame Class
沒有留言:
張貼留言