The wxPython is GUI(Graphical User Interface) toolkit, comprising a set of Python bindings to the wxWidgets library.
As you may know already, wxWidgets is crossplatform C++ application framework for creating user interfaces.
wxPython deos not draw it's own controls, instead it ses the platform's native UI toolkit to create and display the various components.
This results in the wxPython application having a consistent look and feel with other applications on the system.
wxPython allows us write applications that can run on Windows, Linux and Mac OS X as well as other variations of UNIX.
All wxPython applications must create a wxPython application object.
That they create from the
App class defined in the
This is beacuse it is this class that initializes and starts the underlying wxPyhon framework.
Here's the class definition for the
class App(redirect=False, filename=None, useBestVisual=False, clearSigInt=True)
Note that these keyword arguments are optional.
Here are what they stand for:
redirect- Whether to redirect output to
stdout. Defaults to
filename- However, if you chose to redirect output to
stdout, then you can specify an output file.
useBestVisual- Whether the application should try to use the best visuals provided by the underlying toolkit.
clearSigInt- Whether the
SIGINTshould be cleared. If you set it to true, then this allows the application to be terminatedby pressing
Ctrl + Clike other applications.
wx.App class in fact, as the name suggests, represents the wxPython application we are building.
Here are some of the roles of the
Let's now get our feet wet with a Hello World application.
Am using Visual Studio code as my IDE.
import wx class Hello(wx.App): def OnInit(self): wx.MessageBox("Boomerang Nebular is the Coldest place in the Universe","Cosmic News") return True if __name__=="__main__": hello=Hello(False) hello.MainLoop()
First we import the
wx package via
Then create our class called
Hello that is deriving from the
We've then override a method called
OnInit defined in the super class.
OnInit() method will ensure that the application initialization is done and also that the system, toolkit and wxWidgets are fully initialized.
self we are passing refers to the current instance of our class.
Then we create a MeesageBox. This is a dialog for showing messages.
We pass the message to display as well as the title of the messagebox:
wx.MessageBox("Boomerang Nebular is the Coldest place in the Universe","Cosmic News")
Then it is our time to run the application. We check if
Then we instantiate our class which remember inherits from
False tells wxPython not to redirect output. We don't want to direct any output since we want to see in the commandline any errors we encounter.
Lastly we call the
MainLoop method so as to start the event loop.
Starting this event loop and dispatching events to windows instances remember is one of the primary roles of the
As an event loop, the method will run until we destroy the lasp top-level windows or until we exit the app.
wxPython like most GUI toolkits, is event-driven so this
MainLoop is at the heart of it. For each iteration, events are dispatched to all tasks in the window like clicking mouse, resizing window, and even drawing of the components.