Create buttons using a dictionary

hi,

would I want a suggestion,

when I create some buttons I use a dictionary because it is more practical but I need to put them in a certain sequence,

therefore I am forced to perform a sort in this way

buttons = {0:(wx.ID_REFRESH,’&Refresh’,’&Refresh data’),

1:(wx.ID_ADD,’&Add’,’&Add something’),

2:(wx.ID_EDIT,’&Edit’,’&Edit something’),

3:(wx.ID_PRINT,’&Print’,‘Print something’),

4:(wx.ID_OPEN,’&Open’,‘Open something’),

5:(wx.ID_CLOSE,’&Close’,‘Close me’)}

items = buttons.items()

items.sort()

for k,v in items:

b = wx.Button(panel, v[0], v[1], size=(80,-1))

b.SetToolTipString(v[2])

b.Bind(wx.EVT_BUTTON, self.OnClick)

a best way exists?

I attach a demo.

regards

sampleapp.py (2.16 KB)

Hi,

···

On Thu, Aug 2, 2012 at 1:20 PM, beppe giuseppecostanzi@gmail.com wrote:

hi,

would I want a suggestion,

when I create some buttons I use a dictionary because it is more practical but I need to put them in a certain sequence,

therefore I am forced to perform a sort in this way

buttons = {0:(wx.ID_REFRESH,‘&Refresh’,‘&Refresh data’),

1:(wx.ID_ADD,‘&Add’,‘&Add something’),

2:(wx.ID_EDIT,‘&Edit’,‘&Edit something’),

3:(wx.ID_PRINT,‘&Print’,‘Print something’),

4:(wx.ID_OPEN,‘&Open’,‘Open something’),

5:(wx.ID_CLOSE,‘&Close’,‘Close me’)}

items = buttons.items()

items.sort()

for k,v in items:

b = wx.Button(panel, v[0], v[1], size=(80,-1))

b.SetToolTipString(v[2])

b.Bind(wx.EVT_BUTTON, self.OnClick)

a best way exists?

I attach a demo.

regards

I wrote about one method I found for “sorting” dictionaries that you can find here: http://www.blog.pythonlibrary.org/2012/06/19/python-201-how-to-sort-a-dictionary-by-value/

If you’re using Python 2.7, you might want to check out OrderedDicts from the collections module: http://docs.python.org/library/collections.html#collections.OrderedDict


Mike Driscoll

Blog: http://blog.pythonlibrary.org

Have you thought of just putting them in a list of tupples, I think that
the order will stay the same as declared which would simplify the above to:

buttons = [ # Note: Put the buttons in the order required as (ID, Label,
Help Text),
                   (wx.ID_REFRESH, '&Refresh', 'Refresh data'),
                   (wx.ID_ADD, '&Add', 'Add something'),
                   (wx.ID_EDIT, '&Edit', 'Edit something'),
                   (wx.ID_PRINT, '&Print', 'Print something'),
                   (wx.ID_OPEN, '&Open', 'Open something'),
                   (wx.ID_CLOSE, '&Close', 'Close me'),
                   ]
        
        for (id,label, help_text) in buttons:
            b = wx.Button(panel, id, label, size=(80, -1))
            b.SetToolTipString(help_text)
            b.Bind(wx.EVT_BUTTON, self.OnClick)
            sizer.Add(b)

Gadget/Steve

···

On 02/08/2012 7:20 PM, beppe wrote:

hi,
would I want a suggestion,
when I create some buttons I use a dictionary because it is more
practical but I need to put them in a certain sequence,
therefore I am forced to perform a sort in this way

buttons = {0:(wx.ID_REFRESH,'&Refresh','&Refresh data'),
                   1:(wx.ID_ADD,'&Add','&Add something'),
                   2:(wx.ID_EDIT,'&Edit','&Edit something'),
                   3:(wx.ID_PRINT,'&Print','Print something'),
                   4:(wx.ID_OPEN,'&Open','Open something'),
                   5:(wx.ID_CLOSE,'&Close','Close me')}
        
        items = buttons.items()
        items.sort()
        
        for k,v in items:
            b = wx.Button(panel, v[0], v[1], size=(80,-1))
            b.SetToolTipString(v[2])
            b.Bind(wx.EVT_BUTTON, self.OnClick)

a best way exists?
I attach a demo.
regards
--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

Hi Steve, surely yours is the simplest solution.

Mike your article is very interesting even if I will need time to digest it

regards

beppe

···

Il giorno giovedì 2 agosto 2012 21:40:30 UTC+2, Gadget Steve ha scritto:

On 02/08/2012 7:20 PM, beppe wrote:

hi,

would I want a suggestion,
when I create some buttons I use a dictionary because it is more

practical but I need to put them in a certain sequence,

therefore I am forced to perform a sort in this way

buttons = {0:(wx.ID_REFRESH,‘&Refresh’,‘&Refresh data’),

               1:(wx.ID_ADD,'&Add','&Add something'),
               2:(wx.ID_EDIT,'&Edit','&Edit something'),
               3:(wx.ID_PRINT,'&Print','Print something'),
               4:(wx.ID_OPEN,'&Open','Open something'),
               5:(wx.ID_CLOSE,'&Close','Close me')}
    items = buttons.items()
    items.sort()
    for k,v in items:
        b = wx.Button(panel, v[0], v[1], size=(80,-1))
        b.SetToolTipString(v[2])
        b.Bind(wx.EVT_BUTTON, self.OnClick)

a best way exists?

I attach a demo.

regards


To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

Have you thought of just putting them in a list of tupples, I think that

the order will stay the same as declared which would simplify the above to:

buttons = [ # Note: Put the buttons in the order required as (ID, Label,

Help Text),

               (wx.ID_REFRESH, '&Refresh', 'Refresh data'),

               (wx.ID_ADD, '&Add', 'Add something'),

               (wx.ID_EDIT, '&Edit', 'Edit something'),

               (wx.ID_PRINT, '&Print', 'Print something'),

               (wx.ID_OPEN, '&Open', 'Open something'),

               (wx.ID_CLOSE, '&Close', 'Close me'),

               ]

   
    for (id,label, help_text) in buttons:

        b = wx.Button(panel, id, label, size=(80, -1))

        b.SetToolTipString(help_text)

        b.Bind(wx.EVT_BUTTON, self.OnClick)

        sizer.Add(b)

Gadget/Steve