Hi

I'm trying to create a dialog box with GenBitmapTextButton buttons. When I create the dialog using the standard button, I can use the enter key to push the button, and the escape key to cancel the dialog.
However, when I use the GenBitmapTextButton, neither the enter nor the escape key work. The enter key behaves in the same way as the tab key. I've included a skeleton example below that illustrates the issue (to run the code you merely need to include two images called ok.png and cancel.png in the same directory as the file containg the code).
Do I need to bind some event to the button/dialog to make this work in the same manner as the simple case?

I'm using python version 2.5 and wxpython version 2.8.9.2 on windows

Any help would be greatly appreciated.

import wx
import wx.lib.buttons

class SimpleDialog(wx.Dialog):
    def __init__(self, text):
        wx.Dialog.__init__(self, parent=None, id=wx.ID_ANY, title="SimpleDialog", pos=wx.DefaultPosition, size=(250,150))

        wx.StaticText(self, -1, text, wx.Point(15, 20))
        
        okButton = wx.Button(self, wx.ID_OK, ' OK ', wx.Point(35, 90), wx.DefaultSize)
        okButton.SetDefault()
        
        cancelButton = wx.Button(self, wx.ID_CANCEL, ' Cancel ', wx.Point(135, 90), wx.DefaultSize)


class BitmapDialog(wx.Dialog):
    def __init__(self, text):
        wx.Dialog.__init__(self, parent=None, id=wx.ID_ANY, title="BitmapDialog", pos=wx.DefaultPosition, size=(250,150))

        wx.StaticText(self, -1, text, wx.Point(15, 20))
        
        okButton = wx.lib.buttons.GenBitmapTextButton(self, wx.ID_OK, wx.Bitmap('ok.png'), ' OK ', (15, 80), (100, -1))
        okButton.SetBezelWidth(3)
        okButton.SetDefault()

        cancelButton = wx.lib.buttons.GenBitmapTextButton(self, wx.ID_CANCEL, wx.Bitmap('cancel.png'), ' Cancel ', (130, 80), (100, -1))
        cancelButton.SetBezelWidth(3)


if __name__ == '__main__':
    
    app = wx.PySimpleApp()
    dlg1 = SimpleDialog("SimpleDialog Text")
    
    if dlg1.ShowModal() == wx.ID_OK:
        print "SimpleDialog OK"
    else:
        print "SimpleDialog Cancel"
    dlg1.Destroy()
    
    dlg2 = BitmapDialog("BitmapDialog Text")
    if dlg2.ShowModal() == wx.ID_OK:
        print "BitmapDialog OK"
    else:
        print "BitmapDialog Cancel"
    dlg2.Destroy()

    app.MainLoop()

Recommended Answers

All 2 Replies

In order to make buttons behave like standard buttons in your dialog box, you simply create an image button from a standard button and an image:

class BitmapDialog(wx.Dialog):
    def __init__(self, text):
        wx.Dialog.__init__(self, None, -1, title="BitmapDialog", 
            size=(250,150))
        
        bmp = wx.Bitmap('ok.png')
        w, h = bmp.GetSize()
        x = 35
        y = 90
        wx.StaticBitmap(self, -1, bmp,
            pos=(x, y))
        okButton = wx.Button(self, wx.ID_OK, ' OK ', 
            pos=(x+w, y), size=(w*2, h))
        okButton.SetDefault()

        bmp = wx.Bitmap('cancel.png')
        w, h = bmp.GetSize()
        x = 135
        y = 90
        wx.StaticBitmap(self, -1, bmp,
            pos=(x, y))
        cancelButton = wx.Button(self, wx.ID_CANCEL, ' Cancel ', 
            pos=(x+w, y), size=(w*2, h))

Actually, the way these buttons respond to keystrokes depends on the OS.

Thanks Ene.
This does seem to be the only solution.
I'll play around with the layout until it looks right. The GenBitmapTextButton doesn't have the same class hierarchy as the BitmapButton, and as a result it doesn't support the features of the standard button.
Another thing I noticed was how the GenButton doesn't support shortcut keys. "&OK" on the standard button is displayed as OK, but in the GenButton it is &OK.
What you gain in prettiness in the GenButton you lose in functionality

Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.