Tag Archives: wxPython

Creating a Calculator with wxPython

A lot of beginner tutorials start with “Hello World” examples. There are plenty of websites that use a calculator application as a kind of “Hello World” for GUI beginners. Calculators are a good way to learn because they have a set of widgets that you need to lay out in an orderly fashion. They also require a certain amount of logic to make them work correctly. For this calculator, let’s focus on being able to do the following:

  • Addition
  • Subtraction
  • Multiplication
  • Division

I think that supporting these four functions is a great starting place and also give you plenty of room for enhancing the application on your own. Continue reading Creating a Calculator with wxPython

Less than 2 Days to Go on wxPython Book Kickstarter

My latest book, Create GUI Applications with wxPython, is coming along nicely. I just wanted to let my readership know that the Kickstarter for it is coming to a close in a little less than 2 days.

If you’d like to get a copy at a cheaper price than it will be when it is released in May later this year, the Kickstarter is really the way to go. You can check out the current table of contents in this post from last week.

Thanks for your support!

Table of Contents for Creating GUI Applications Book

We are coming into the last week of the Kickstarter and I thought I would give you all a quick update. I finished writing up the chapter on creating a calculator today and got started on chapter 7.

Creating GUI Applications with wxPython

I also wanted to let you know what the current table of contents looks like right now:

  • Chapter 1 – Intro to wxPython
  • Chapter 2 – Creating an Image Viewer
  • Chapter 3 – Enhancing the Image Viewer
  • Chapter 4 – Creating a Database Viewer
  • Chapter 5 – Database Editing with wxPython
  • Chapter 6 – Calculator
  • Chapter 7 – Archiver (tarball creation utility)
  • Chapter 8 – MP3 Tag Editor
  • Chapter 9 – XML Editor
  • Chapter 10 – NASA Image Downloader / Search Tool
  • Chapter 11 – PDF Merger / Splitter

There will also be a chapter on creating executables and installers for your application and a couple of appendixes.

Thanks so much for your support!

Mike

Creating GUI Applications with wxPython Kickstarter

I am pleased to announce my latest book project, Creating GUI Applications with wxPython which I am running a Kickstarter campaign for.

Creating GUI Applications with wxPython is a book that will teach you how to use wxPython to create applications by actually creating several mini-programs. I have found that while learning how the various widgets work in wxPython is valuable, it is even better to learn by creating a simple application that does something useful.

The code in this book will be targeted for Python 3 only using wxPython 4.

Creating GUI Applications with wxPython cover

For more information, please check out the Kickstarter.

wxPython: Changing Custom Renderers for Columns / Rows

The wxPython GUI toolkit has a very rich and powerful Grid widget that I have written about previously on this blog. It allows you to create sheets of cells similar to those in Microsoft Excel.

There is also a neat mixin that allows you to apply a custom renderer to the labels on the columns and rows of the grid.

Let’s take a look at that and see how it works:

import wx
import wx.grid as grid
import wx.lib.mixins.gridlabelrenderer as glr
 
class MyGrid(grid.Grid, glr.GridWithLabelRenderersMixin):
 
    def __init__(self, *args, **kw):
        grid.Grid.__init__(self, *args, **kw)
        glr.GridWithLabelRenderersMixin.__init__(self)
 
class MyColLabelRenderer(glr.GridLabelRenderer):
 
    def __init__(self, bgcolor):
        self._bgcolor = bgcolor
 
    def Draw(self, grid, dc, rect, col):
        dc.SetBrush(wx.Brush(self._bgcolor))
        dc.SetPen(wx.TRANSPARENT_PEN)
        dc.DrawRectangle(rect)
        hAlign, vAlign = grid.GetColLabelAlignment()
        text = grid.GetColLabelValue(col)
        self.DrawBorder(grid, dc, rect)
        self.DrawText(grid, dc, rect, text, hAlign, vAlign)
 
class MyPanel(wx.Panel):
 
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
 
        grid = MyGrid(self, size=(100, 100))
        grid.CreateGrid(numRows=10, numCols=10)
 
        for col in range(0, 10, 3):
            grid.SetColLabelRenderer(
                col+0, MyColLabelRenderer('#e0ffe0'))
            grid.SetColLabelRenderer(
                col+1, MyColLabelRenderer('#e0e0ff'))
            grid.SetColLabelRenderer(
                col+2, MyColLabelRenderer('#ffe0e0'))
 
        main_sizer = wx.BoxSizer(wx.VERTICAL)
        main_sizer.Add(grid, 1, wx.EXPAND)
        self.SetSizer(main_sizer)
 
class MyFrame(wx.Frame):
 
    def __init__(self):
        wx.Frame.__init__(self, None, title='Custom Grid Renderers')
        panel = MyPanel(self)
        self.Show()
 
if __name__ == '__main__':
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

Let’s break this down a bit. You will notice at the top of the code that we need to import the Grid widget separately in wxPython. We also need to import a mixin called GridWithLabelRenderersMixin. We subclass the Grid class and add in the mixin and then initialize both.

Next we create a subclass of GridLabelRenderer, which is also from the mixin. This allows us to create a spacing Draw method that will give us the ability to apply different colors or fonts to the labels in our Grid. In this case, I just made it so that we could change the color of the text in the labels.

The last piece of code that we are interested in is in the MyPanel class where we actually instantiate the Grid and change the color of the background of the labels in the columns. Here is what the grid ended up looking like:

wxPython Grid widget with colored columns
Custom Grid Column Renderers

Wrapping up

The wxPython toolkit has dozens of pre-built widgets that you can use to create cross-platform user interfaces. The wxPython demo has a much more involved example than this article does that you might also find interesting. If you haven’t given wxPython a try, you really should go get it. It is pip installable from PyPI and compatible with Python 3.

Black Friday / Cyber Monday Sale 2018

This week I am putting my 2 most recent self-published books on Sale starting today through November 26th.

ReportLab – PDF Processing with Python is available for $9.99:

JupyterLab 101 is available for $9.99:

You can also get my book, wxPython Recipes, from Apress for $7 for a limited time with the following coupon code: cyberweek18.

Python Interviews is $10 right now too!

wxPython: How to Open a Second Window / Frame

I see questions relating to the title of this article a lot. How do I open a second frame / window? How do I get all the frames to close when I close the main application? When you are first learning wxPython, these kinds of questions can be kind of hard to find answers for as you aren’t familiar enough with the framework or the terminology to know how to search for the answers.

Hopefully this article will help. We will learn how to open multiple frames and how to make them all close too. Let’s get started! Continue reading wxPython: How to Open a Second Window / Frame

wxPython 101: Creating a Splash Screen

A common UI element that you used to see a lot of was the Splash Screen. A splash screen is just a dialog with a logo or art on it that sometimes includes a message about how far along the application has loaded. Some developers use splash screens as a way to tell the user that the application is loading so they don’t try to open it multiple times.

wxPython has support for creating splash screens. In versions of wxPython prior to version 4, you could find the splash screen widget in wx.SplashScreen. However in wxPython’s latest version, it has been moved to wx.adv.SplashScreen.

Let’s look at a simple example of the Splash Screen:

import wx
import wx.adv
 
class MyFrame(wx.Frame):
 
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial", size=(500,500))
 
        bitmap = wx.Bitmap('py_logo.png')
        splash = wx.adv.SplashScreen(
                     bitmap, 
                     wx.adv.SPLASH_CENTER_ON_SCREEN|wx.adv.SPLASH_TIMEOUT, 
                     5000, self)
        splash.Show()
 
        self.Show()
 
 
# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

Here we create a subclass of wx.Frame and we load up an image using wx.Bitmap. You will note that wx.Bitmap does not actually require you to only load bitmaps as I am using a PNG here. Anyway, the next line instantiates our splash screen instance. Here we pass it the bitmap we want to show, a flag to tell it how to position itself, a timeout in milliseconds for how long the splash screen should show itself and what its parent should be. These are all required arguments.

There are also three additional arguments that the splash screen widget can accept: pos, size and style. You will note that in this example we tell the splash screen to center itself onscreen. We could also tell it to center on its parent via SPLASH_CENTRE_ON_PARENT.

You will, of course, need to modify this example to use an image of your own.


Wrapping Up

The splash screen is actually pretty useful if you have an application that takes a long time to load. You can easily use it to distract the user and give the illusion that your application is still responsive even when it hasn’t fully loaded yet. Give it a try and see what you think.


Related Reading

wxPython: Set Which Display the Frame is on

The other day, I saw an interesting question in the wxPython IRC channel. They were asking if there was a way to set which display their application would appear on. Robin Dunn, the creator of wxPython, gave the questioner some pointers, but I decided to go ahead and write up a quick tutorial on the topic.

The wxPython toolkit actually has all the bits and pieces you need for this sort of thing. The first step is getting the combined screen size. What I mean by this is asking wxPython what it thinks is the total size of the screen. This would be the total width and height of all your displays combined. You can get this by calling wx.DisplaySize(), which returns a tuple. If you would like to get individual display resolutions, then you have to call wx.Display and pass in the index of the display. So if you have two displays, then the first display’s resolution could be acquired like this:

index = 0
display = wx.Display(index)
geo = display.GetGeometry()

Let’s write up a quick little application that has a single button that will just switch which display the application is on. Continue reading wxPython: Set Which Display the Frame is on

How to Use wxPython Demo Code Outside the Demo

Every now and then, someone will ask about how they can run the demo code from wxPython’s demo outside of the demo. In other words, they wonder how you can extract the code from the demo and run it in your own. I think I wrote about this very topic quite some time ago on the wxPython wiki, but I thought I should write on the topic here as well.


What to do about the log

The first issue that I always see is that the demo code is riddled with calls to some kind of log. It’s always writing to that log to help the developer see how different events get fired or how different methods get called. This is all well and good, but it makes just copying the code out of the demo difficult. Let’s take the code from the wx.ListBox demo as an example and see if we can make it work outside of the demo. Here is the demo code: Continue reading How to Use wxPython Demo Code Outside the Demo