WxPython Introduction and Hello World


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.

wxPython Application Object

All wxPython applications must create a wxPython application object.

That they create from the App class defined in the wx package.

This is beacuse it is this class that initializes and starts the underlying wxPyhon framework.

Here's the class definition for the wx.App class:

class App(redirect=False, filename=None, useBestVisual=False, clearSigInt=True)

Note that these keyword arguments are optional.

Here are what they stand for:

  1. redirect - Whether to redirect output to stdout. Defaults to False.
  2. filename - However, if you chose to redirect output to stdout, then you can specify an output file.
  3. useBestVisual - Whether the application should try to use the best visuals provided by the underlying toolkit.
  4. clearSigInt - Whether the SIGINT should be cleared. If you set it to true, then this allows the application to be terminatedby pressing Ctrl + C like other applications.

The wx.App class in fact, as the name suggests, represents the wxPython application we are building.

Roles of wx.App class:

Here are some of the roles of the wx.App class:

  1. It first bootstraps the wxPython system then initializes the underlying Graphical User Interface toolkit.
  2. It is responsible for setting and getting our application-wide properties.
  3. It is responsible for implementing the native windowing system main message or event loop and the dispatch of events to window instances.

Let's now get our feet wet with a Hello World application.

Am using Visual Studio code as my IDE.

hello.py

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()        

Result

wxPython Hello World

Explanation

Import wx

First we import the wx package via import statement:

import wx

Create Class, Derive from wx.App

Then create our class called Hello that is deriving from the wx.App class:

class Hello(wx.App):

Override OnInit()

We've then override a method called OnInit defined in the super class.

    def OnInit(self):

This OnInit() method will ensure that the application initialization is done and also that the system, toolkit and wxWidgets are fully initialized.

The self we are passing refers to the current instance of our class.

Then we create a MeesageBox. This is a dialog for showing messages.

Create MessageBox

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")

Run the application

Then it is our time to run the application. We check if if __name__=="__main__".

Then we instantiate our class which remember inherits from wx.App:

    hello=Hello(False)

The 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.

Start the MainLoop

Lastly we call the App class's 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 wx.App class.

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.

Best regards.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think

Dear readers drop us your comments below. We are building a community of students and learners. Start by dropping us your suggestions below. What tutorials do you want us to do for example? Where can we improve? What are some awesome resources out there? Do you have any code you want to share with us?
By the way that example or snippet you have lying in your computer can really help beginner programmers. We can share it here with other students.

Next Post