Tag Archives: wxPython

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

wxPython Recipes Book Contest

I recently had my self-published book, “wxPython Cookbook” picked up by Apress and republished as wxPython Recipes. Since they gave me a few complimentary paperback copies, I have decided to do a little contest.

Rules

  • Post a comment telling me why you would want a copy
  • The most clever or heartfelt commenter will be chosen by me

The contest will run starting now until Monday, January 15th @ 11:59 p.m. CST.

The winner will be contacted by yours truly and I will sign the book and ship it wherever you want me to.

For those of you who want to purchase the book, Apress gave me a lame 20% off coupon that you can use for either the eBook or Paperback on their website: wx20

wxPython Recipes Book Release

I was contacted earlier this year by Apress about republishing my book, wxPython Cookbook, under their branding. I thought it might be fun to see what I could learn from a publisher so I went with them as I have enjoyed several of their books in the past. The biggest change to the book is that I ended up grouping recipes into chapters instead of having each recipe be a stand-alone chapter. I also added a few new recipes to help fill in when some chapters weren’t easily sorted into groups.

Anyway, Apress just released the book in the past couple of days:

You can find the book over on Amazon or on the Apress website. You can also see a preview of the book on Google.

You can get 20% off of the book from Apress by using the following code: wx20. This code is good on the paperback and the eBook versions of the book until June 2018.

The code for the book is hosted on Apress’s Github account. I also host a copy on Github.

Regardless, feel free to check it out. If you already bought a copy of the wxPython Cookbook, then you don’t need to get this one too since it’s basically the same thing with a bit more polish and a handful of new recipes. I have plans for some other books that I will be self-publishing hopefully in 2018, so keep an eye on the blog for news about that!

wxPython: Moving items in ObjectListView

I was recently asked about how to implement drag-and-drop of items in a wx.ListCtrl or in ObjectListView. Unfortunately neither control has this built-in although I did find an article on the wxPython wiki that demonstrated one way to do drag-and-drop of the items in a ListCtrl.

However I did think that implementing some buttons to move items around in an ObjectListView widget should be fairly easy to implement. So that’s what this article will be focusing on.


Changing Item Order

If you don’t have wxPython and ObjectListView installed, then you will want to use pip to install them:

pip install wxPython objectlistview

Once that is done, open up your favorite text editor or IDE and enter the following code: Continue reading wxPython: Moving items in ObjectListView

wxPython: Drag and Drop an Image onto Your Application

I recently came across a question on StackOverflow where the user wanted to know how to drag images onto their image control in wxPython and have the dragged image resize into a thumbnail. This piqued my interest and I decided to figure out how to do it.

I knew that you could create a thumbnail in Python using the Pillow package. So if you’d like to follow along you will need to install Pillow and wxPython with pip:

pip install Pillow wxPython

Now that we have the latest versions of the packages we need, we can write some code. Let’s take a look: Continue reading wxPython: Drag and Drop an Image onto Your Application

wxPython: All About Accelerators

The wxPython toolkit supports using keyboard shortcuts via the concept of Accelerators and Accelerator Tables. You can also bind directly to key presses, but in a lot of cases, you will want to go with Accelerators. The accelerator gives to the ability to add a keyboard shortcut to your application, such as the ubiquitous “CTRL+S” that most applications use for saving a file. As long as your application has focus, this keyboard shortcut can be added trivially.

Note that you will normally add an accelerator table to your wx.Frame instance. If you happen to have multiple frames in your application, then you may need to add an accelerator table to multiple frames depending on your design.

Let’s take a look at a simple example: Continue reading wxPython: All About Accelerators