[wxPython-users] widget suggestion / question

Zach Heilig zach at gra.midco.net
Sun Dec 2 23:18:51 PST 2007


Mark Erbaugh wrote:

> In Borland Delphi, the individual widgets (components) had a spare field
> that could be used to store an integer for whatever purpose the
> developer wanted.  I would like to see something like this in wx. Here's
> where I could use something like this. I designed a form with a series
> of buttons. Several of the buttons could share the same event handler if
> the event handler could figure out which button invoked it.  I supposed
> I could use reflection to figure out which button, but it would be nice
> if the event handler could check this spare field in the button.
>   
You could do something like this (using a number keypad, as an example):    

import wx


class Keypad(wx.Dialog):
    def __init__(self, parent):
        wx.Dialog.__init__(self, parent, wx.ID_ANY)
        self.text_ctrl = wx.TextCtrl(self, wx.ID_ANY, '')
        button_sizer = wx.GridSizer(cols = 3, hgap = 3, vgap = 3)
        for rows in ( ('7', '8', '9'), ('4', '5', '6'), ('1', '2', '3'), ('00', '0', '.') ):
            for label in rows:
                button = wx.Button(self, wx.ID_ANY, label)
                button.SetMinSize((30,30))
                self.Bind(wx.EVT_BUTTON, self.button_handler, button)
                button_sizer.Add(button)
        pad_sizer = wx.BoxSizer(wx.VERTICAL)
        pad_sizer.Add(self.text_ctrl, 1, wx.EXPAND)
        pad_sizer.Add(button_sizer)

        self.SetSizer(pad_sizer)
        pad_sizer.Fit(self)
        pad_sizer.SetSizeHints(self)
        self.Layout()

    def button_handler(self, event):
        label = event.GetEventObject().GetLabel()
        self.text_ctrl.AppendText(label)







More information about the wxpython-users mailing list