Category Archives: Python GUI Toolkits

Letting Users Change a wx.ComboBox’s Contents in wxPython

This week I came across someone who was wondering if there was a way to allow the user to edit the contents of a wx.ComboBox. By editing the contents, I mean change the names of the pre-existing choices that the ComboBox contains, not adding new items to the widget.

While editing the contents of the selected item in a ComboBox works out of the box, the widget will not save those edits automatically. So if you edit something and then choose a different option in the ComboBox, the edited item will revert back to whatever it was previously and your changes will be lost.

Let’s find out how you can create a ComboBox that allows this functionality! Continue reading Letting Users Change a wx.ComboBox’s Contents in wxPython

Loading UI Files in Qt for Python

Qt for Python (i.e. PySide2) was announced recently and got me interested in trying to use it to load up a UI file. If you don’t know, PyQt and PySide / PySide2 can use the Qt Creator application to create user interfaces using a drag-and-drop interface. This is actually very similar to the way you would create an interface using Visual Studio. Qt Creator / Designer will generate an XML file with a *.ui extension that you can then load inside of your PySide2 application (or PyQt).

Creating the User Interface

For this example, I opened up Qt Creator and went to File –> “New File or Project”. Then I chose the “Qt Widgets Application” choice. See screenshot below:

Then I opened up the mainwindow.ui that Qt Creator made for me. You can just double-click it or click on the Design button that should be on the left-hand side of the program. Here’s a screenshot that might help:

I added three widgets to my UI:

  • QLabel
  • QLineEdit
  • QPushButton

Continue reading Loading UI Files in Qt for Python

Getting Started with Qt for Python

The Qt Team recently posted that Qt will now be officially supporting the PySide2 project, which they are calling “Qt for Python”. It will be a complete port of the original PySide, which only supported Qt 4. PySide2 supports Qt 5. Qt for Python will have the following license types: GPL, LGPL and commercial.

PySide2 supports Python 2.7 as well as Python 3.4 – 3.6. There are snapshot wheel builds available here.Let’s say we downloaded the Windows Python wheel. To install it, you can use pip like this:

python -m pip install PySide2-5.11.0a1-5.11.0-cp36-cp36m-win_amd64.whl

Once you have PySide2 installed, we can get started by looking at a really simple example:

import sys
from PySide2.QtWidgets import QApplication, QLabel

if __name__ == '__main__':
    app = QApplication([])
    label = QLabel("Qt for Python!")

This code will create our application object (QApplication) and a QLabel to go on it. When you run app.exec_(), you start PySide2’s event loop. Since we do not specify a size for the label or the application, the size of the application defaults to be just large enough to fit the label on-screen:

Continue reading Getting Started with Qt for Python

PyDev of the Week: Bryan Oakley

This week we welcome Bryan Oakley as our PyDev of the Week! I’ve met Bryan a few times in various Python-related forums and developer hangouts online. He’s a master at Tkinter and has answered a lot of questions about it (and Python) of StackOverflow and other places. I hope you will enjoy getting to know more about as much as I did.

Continue reading PyDev of the Week: Bryan Oakley

eBook Review: Creating Apps in Kivy

Kivy is a neat package that allows Python developers to create user interfaces on mobile devices. You can also deploy the applications to desktops too. This is the second book I’ve seen put out on the subject. The first book, Kivy – Interactive Applications in Python by Roberto Ulloa came out last year from Packt Publishing. This year, we have Dusty Phillips’ work, Creating Apps in Kivy from O’Reilly. I will be reviewing the PDF version of the book.

Quick Review

  • Why I picked it up:I picked this book up because I like the author’s previous work, Python 3 Object Oriented Programming
  • Why I finished it: The book is pretty short and it’s interesting
  • I’d give it to: Someone who already knows Python

Continue reading eBook Review: Creating Apps in Kivy

wxPython 2.9 and the Newer Pubsub API: A Simple Tutorial

NOTE: This article is for wxPython 2.9-3.0. If you are using wxPython 4, you should go to my newer article

Several years ago, I wrote a tutorial about wxPython 2.8 and its built-in pubsub module which you can read here. Back then, a new API for pubsub was added in wxPython that could be enabled by doing the following:

import wx.lib.pubsub.setupkwargs
from wx.lib.pubsub import pub

The old way of importing pubsub was to do the following:

from wx.lib.pubsub import Publisher

Now in wxPython 2.9, it has changed to this:

from wx.lib.pubsub import pub

Thus you cannot use the code in my old tutorial any more and expect it to work in the latest version of wxPython. So it’s time to update the tutorial a bit. Continue reading wxPython 2.9 and the Newer Pubsub API: A Simple Tutorial

wxPython: How to Communicate with Your GUI via sockets

I sometimes run into situations where it would be nice to have one of my Python scripts communicate with another of my Python scripts. For example, I might want to send a message from a command-line script that runs in the background to my wxPython GUI that’s running on the same machine. I had heard of a solution involving Python’s socket module a couple of years ago, but didn’t investigate it until today when one of my friends was asking me how this was done. It turns out Cody Precord has a recipe in his wxPython Cookbook that covers this topic fairly well. I’ve taken his example and done my own thing with it for this article. Continue reading wxPython: How to Communicate with Your GUI via sockets

PySide: Creating a Currency Converter

I am currently reading through Mark Summerfield’s book on PyQt, Rapid GUI Programming with Python and Qt and thought it would be fun to take some of the example applications in it and convert them to PySide. So I’ll be creating a series of articles where I’ll show the original PyQt examples from the book and then convert them to PySide and probably add something of my own to the code. The book doesn’t really get in Qt GUI coding until chapter 4 where the author creates a fun little currency converter. Come along and enjoy the fun! Continue reading PySide: Creating a Currency Converter

wxPython: How to Get Children Widgets from a Sizer

The other day, I stumbled across a question on StackOverflow asking how to get the children widgets of a BoxSizer. In wxPython, you would expect to call the sizer’s GetChildren() method. However, this returns a list of SizerItems objects rather than a list of the actual widgets themselves. You can see the difference if you call a wx.Panel’s GetChildren() method. Now I don’t ask a lot of questions on the wxPython users group list, but I was curious about this one and ended up receiving a quick answer from Cody Precord, author of the wxPython Cookbook and Editra. Anyway, he ended up pointing me in the right direction and I came up with the following bit of code:
Continue reading wxPython: How to Get Children Widgets from a Sizer

wxPython: How to make “flashing text”

People keep on asking fun wxPython questions on StackOverflow. Today they wanted to know how to make “flashing text” in wxPython. That’s actually a pretty easy thing to do. Let’s take a look at some simple code:

import random
import time
import wx

class MyPanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.font = wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
        self.flashingText = wx.StaticText(self, label="I flash a LOT!")
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.update, self.timer)
    def update(self, event):
        now = int(time.time())
        mod = now % 2
        print now
        print mod
        if mod:
            self.flashingText.SetLabel("Current time: %i" % now)
            self.flashingText.SetLabel("Oops! It's mod zero time!")
        colors = ["blue", "green", "red", "yellow"]
class MyFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, title="Flashing text!")
        panel = MyPanel(self)
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()

Basically all you need is a wx.StaticText instance and a wx.Timer. In this example, the text will “flash” once a second. By flash, we mean it will change colors AND the text itself will change. The original person who made this question wanted to know how to display the time using Python’s time.time() method and they wanted the message to change depending on whether or not the modulus of the time by 2 was equal to zero. I realize that looks a little odd, but I’ve actually used that idea in some of my own code. Anyway, this worked for me on Windows 7 with Python 2.6.6 and wxPython

Note that sometimes the SetForegroundColour method doesn’t work on all widgets across all platforms as the native widget doesn’t always allow the color to be changed, so your mileage may vary.