GUI Toolkits


The PySide GUI toolkit for Python has several standard dialogs and message boxes that you can use as-is. You can also create custom dialogs, but we’ll be saving that for a future article. In this post, we will cover the following dialogs:

  • Color Dialog
  • File Dialog
  • Font Dialog
  • Input Dialog
  • Print & Print Preview Dialogs

We will also cover PySide’s Message Boxes. All code in this article was tested on Windows 7 Professional with PySide 1.2.2 and Python 2.6.6. Now let’s get to it! (more…)

As I learn PyQt and PySide, I am writing some tutorials to help my fellow travelers. Today we’ll be looking at how to connect multiple widgets to the same slot. In other words, we’ll be binding the widgets signals (basically events) to slots (i.e. callables like functions, methods) which are better known as “event handlers”. Yes, I know in PySide land that you don’t call it that. It’s a SLOT, not an event handler. But I digress. Anyway, there are two ways to approach this. One is to use functools and its partial class to pass parameters or to use PySide’s introspection abilities to grab that information from the widget that did the calling. There’s actually at least one other method that is very similar to functools.partial and that is to use the infamous anonymous function utility known as lambda. Let’s take a look at how all these methods can be done! (more…)

Fabio Zadrozny, the primary developer behind PyDev has started a campaign on Indiegogo to fund continuing development of PyDev. In case you’re new to Python development, PyDev is a plugin for Eclipse that provides a nice Integrated Development Environment (IDE) for Python, Jython and IronPython in the Eclipse environment.

You can read about why Fabio decided to go the Indiegogo route for funding on his blog. While I personally don’t use PyDev, several of my co-workers really like it. I think supporting our fellow Python developers is a good thing, so if you have some extra cash to spare for a pretty great open source project, then you might want to support this campaign.

This week, I needed to figure out how to attach an event handler that would fire when I double-clicked an item (i.e. row) in an ObjectListView widget that was in LC_REPORT mode. For some reason, there isn’t an obvious mouse event for that. There is an EVT_LIST_ITEM_RIGHT_CLICK and an EVT_LIST_ITEM_MIDDLE_CLICK, but nothing for LEFT clicks of any sort. After a bit of searching on Google, I found that I can get it to work by using EVT_LIST_ITEM_ACTIVATED. This will fire when an item is double-clicked and when an item is selected and the user presses ENTER. Here’s a code example:

import wx
from ObjectListView import ObjectListView, ColumnDefn
 
########################################################################
class Results(object):
    """"""
 
    #----------------------------------------------------------------------
    def __init__(self, tin, zip_code, plus4, name, address):
        """Constructor"""
        self.tin = tin
        self.zip_code = zip_code
        self.plus4 = plus4
        self.name = name
        self.address = address
 
 
########################################################################
class DCPanel(wx.Panel):
    """"""
 
    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent=parent)
 
        mainSizer = wx.BoxSizer(wx.VERTICAL)
 
        self.test_data = [Results("123456789", "50158", "0065", "Patti Jones",
                                  "111 Centennial Drive"),
                          Results("978561236", "90056", "7890", "Brian Wilson",
                                  "555 Torque Maui"),
                          Results("456897852", "70014", "6545", "Mike Love", 
                                  "304 Cali Bvld")
                          ]
        self.resultsOlv = ObjectListView(self, style=wx.LC_REPORT|wx.SUNKEN_BORDER)
        self.resultsOlv.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.onDoubleClick)
 
        self.setResults()
 
        mainSizer.Add(self.resultsOlv, 1, wx.EXPAND|wx.ALL, 5)
        self.SetSizer(mainSizer)
 
    #----------------------------------------------------------------------
    def onDoubleClick(self, event):
        """
        When the item is double-clicked or "activated", do something
        """
        print "in onDoubleClick method"
 
    #----------------------------------------------------------------------
    def setResults(self):
        """"""
        self.resultsOlv.SetColumns([
            ColumnDefn("TIN", "left", 100, "tin"),
            ColumnDefn("Zip", "left", 75, "zip_code"),
            ColumnDefn("+4", "left", 50, "plus4"),
            ColumnDefn("Name", "left", 150, "name"),
            ColumnDefn("Address", "left", 200, "address")
            ])
        self.resultsOlv.CreateCheckStateColumn()
        self.resultsOlv.SetObjects(self.test_data)
 
 
########################################################################
class DCFrame(wx.Frame):
    """"""
 
    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, parent=None, title="Double-click Tutorial")
        panel = DCPanel(self)
 
#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = DCFrame()
    frame.Show()
    app.MainLoop()

Pretty straight-forward, right? I hope it’ll help you if you ever need to know how to do this. This method should work with a ListCtrl as well.

Today on StackOverflow I saw someone who wanted to know how to drag a file from a wx.ListCtrl onto their Desktop or somewhere else in the file system. They were using the file manager skeleton from zetcode, but couldn’t figure out how to add the DnD portion. After a bit of searching and hacking, I came up with this based on something Robin Dunn mentioned in a forum. (more…)

Today we’re going to take a look at Tkinter! I was curious about how one would go about hiding a frame and then re-showing it using Tkinter and I kept finding threads (like this one) that talked about using withdraw() and deiconify() but didn’t really provide any usable code. In wxPython, I did this sort of thing using pubsub. We’ll go over three different versions of how to hide and show the root frame. (more…)

Robin Dunn, creator and mastermind behind wxPython, announced today on his blog and the wxPython-dev mailing list that he had gotten wxPython 2.9 (Phoenix) to build successfully for Python 3.2 on Mac. In fact, he posted a Quicktime video that shows the build and the tests running in Python 3! According to wxPython-dev, once they have some Python 3 buildbot slaves set up, then snapshot builds can be made and posted here.

I’m pretty excited! Now if only the Python Imaging Library would convert too…

I recently bought Modern Tkinter for Busy Python Developers by Mark Roseman from Amazon and just finished it yesterday. I think it’s pretty new, but I can’t find the release date for it now. Anyway, let’s get on with the review!

Quick Review

  • Why I picked it up: I bought this book because I’d been planning to dig into other Python GUI toolkits anyway and I haven’t seen a new Tkinter book since John Grayson’s Python and Tkinter Programming
  • Why I finished it: It has a pretty good writing style, although the widgets chapters started to drag
  • I’d give it to: Anyone wanting to make their Tkinter applications look more native or learn a little about Tkinters new theming system.

(more…)