Welcome to the wxPython Tutorial Series. In this Tutorial Series we will cover the entirety of wxPython, from it’s basic setup all the way to advanced widgets and special features.
wxPython is a GUI library based of the popular C++ wxWidgets library. It’s a popular GUI library used by many for it’s cross-platform ability, clean UI and powerful and easy to use widgets.
wxPython Tutorial Series
In this tutorial (found below) we will begin by explaining how to setup a basic wxPython window and the various settings that come with it. The other widgets and other features are covered in other separate tutorials to which links can be found below.
A List of Tutorials on Widgets in wxPython:
- Basic Setup + Frames + Panels
- Button Widget
- StaticText Widget
- TextCtlr Widget
- StaticLine Widget
- RadioButton Widget
- RadioBox Widget
- ComboBox Widget
- CheckBox Widget
- ToggleButton Widget
- StatusBar Widget
- MenuBar and MenuButton Widget
- BitMapButton Widget
- StaticBox Widget
The first thing you need to do is download wxPython using
pip, or some other package installer for your OS.
pip install wxpython
Next you just have to make the proper import in your Python code.
If this works without any errors, then your download/installation was successful.
Tutorial: Creating a wxPython Window (Frame)
In this section we’ll fully setup a proper Window using wxPython.
The below code implements a basic implementation of a
wx.Frame, which is used to create the Window. (We are using the object oriented approach here, which is flexible and effective in larger programs).
Within this frame is Panel widget, which is basically your “content window”, where you will be placing all of your widgets. You can create multiple panels as well, basically creating several areas where you can place widgets. The
panel has several parameters, but we will only pass one for the parent, which is the frame. (self refers to the
frame in this context)
import wx class Window(wx.Frame): def __init__(self, title): super().__init__(parent = None, title = title) self.panel = wx.Panel(self) self.Show() app = wx.App() window = Window("WxPython Tutorial") app.MainLoop()
__init__() function takes an extra parameter for the title, otherwise the other parameter options for the
wx.Frame are left to their default values. Since this window is our main Window, the
parent is set to
self.Show() is a method belonging to the Frame Class, which is responsible for displaying the Frame on screen. There will be no output without this line.
Running the above code will produce the following output.
A full list of parameters that the
wx.Frame can take.
frame = wx.Frame(parent, id, title, pos, size, style)
parent: This is the Widget to which it is parented, such as a Panel.
id: Widget ID. Default value is ID_ANY, which gives it the next available ID.
title:The caption/name that appears on the topleft corner of the wxPython Window.
pos:A tuple containing the screen coordinates of where the topleft corner of the Window should begin from.
size:A tuple which defines the dimensions of the area occupied by the Window.
style:Used for styling the Window.
Styling the Window
In this section, we’ll take a look at some Styling options, which will add extra functionality into our Window.
A list of Window Styles:
|wx.DEFAULT_FRAME_STYLE|| A compilation of several styles, |
|wx.ICONIZE||Display the Frame minimized (Windows only)|
|wx.CAPTION|| Puts a Caption Bar on the Frame. You must have this Style on for |
|wx.MINIMIZE||Display the Frame minimized (Windows only)|
|wx.MINIMIZE_BOX||Enables the minimize option on the Window.|
|wx.MAXIMIZE||Display the Frame maximized (Windows and GTK Only)|
|wx.MAXIMIZE_BOX||Enables the maximize option on the Window.|
|wx.CLOSE_BOX||Enables the close option on the Window.|
|wx.STAY_ON_TOP||Makes the Window stay on top of all other windows.|
|wx.SYSTEM_MENU||Displays a system menu containing the list of various windows commands in the window title bar.|
|wx.RESIZE_BORDER||Allows for resizing along the borders of the window.|
A list of useful methods that can be used on the Frame.
|Show()||Displays the Frame on-screen.|
|Centre()||Centers the Frame to the Screen.|
|SetSize(x, y)||Used to change the size of the size of the Frame.|
|Close()||Closes the Frame safely.|
|Destroy()||Destroys the Frame immediately.|
|Disable()||Disables the Frame.|
|Hide()||Hides the window.|
Check out the documentation for the full list of available methods.
This marks the end of the wxPython Tutorial Series. Any suggestions or Contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.