Python GUI Toolkits


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! (more…)

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:
(more…)

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):
        """Constructor"""
        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.flashingText.SetFont(self.font)
 
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.update, self.timer)
        self.timer.Start(1000)
 
    #----------------------------------------------------------------------
    def update(self, event):
        """"""
        now = int(time.time())
        mod = now % 2
        print now
        print mod
        if mod:
            self.flashingText.SetLabel("Current time: %i" % now)
        else:
            self.flashingText.SetLabel("Oops! It's mod zero time!")
        colors = ["blue", "green", "red", "yellow"]
        self.flashingText.SetForegroundColour(random.choice(colors))
 
 
########################################################################
class MyFrame(wx.Frame):
    """"""
 
    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Flashing text!")
        panel = MyPanel(self)
        self.Show()
 
#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

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

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.