wxpython button help

Thread Solved

Join Date: May 2009
Posts: 2
Reputation: fkadri is an unknown quantity at this point 
Solved Threads: 0
fkadri fkadri is offline Offline
Newbie Poster

wxpython button help

 
0
  #1
May 26th, 2009
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.

  1. import wx
  2. import wx.lib.buttons
  3.  
  4. class SimpleDialog(wx.Dialog):
  5. def __init__(self, text):
  6. wx.Dialog.__init__(self, parent=None, id=wx.ID_ANY, title="SimpleDialog", pos=wx.DefaultPosition, size=(250,150))
  7.  
  8. wx.StaticText(self, -1, text, wx.Point(15, 20))
  9.  
  10. okButton = wx.Button(self, wx.ID_OK, ' OK ', wx.Point(35, 90), wx.DefaultSize)
  11. okButton.SetDefault()
  12.  
  13. cancelButton = wx.Button(self, wx.ID_CANCEL, ' Cancel ', wx.Point(135, 90), wx.DefaultSize)
  14.  
  15.  
  16. class BitmapDialog(wx.Dialog):
  17. def __init__(self, text):
  18. wx.Dialog.__init__(self, parent=None, id=wx.ID_ANY, title="BitmapDialog", pos=wx.DefaultPosition, size=(250,150))
  19.  
  20. wx.StaticText(self, -1, text, wx.Point(15, 20))
  21.  
  22. okButton = wx.lib.buttons.GenBitmapTextButton(self, wx.ID_OK, wx.Bitmap('ok.png'), ' OK ', (15, 80), (100, -1))
  23. okButton.SetBezelWidth(3)
  24. okButton.SetDefault()
  25.  
  26. cancelButton = wx.lib.buttons.GenBitmapTextButton(self, wx.ID_CANCEL, wx.Bitmap('cancel.png'), ' Cancel ', (130, 80), (100, -1))
  27. cancelButton.SetBezelWidth(3)
  28.  
  29.  
  30. if __name__ == '__main__':
  31.  
  32. app = wx.PySimpleApp()
  33. dlg1 = SimpleDialog("SimpleDialog Text")
  34.  
  35. if dlg1.ShowModal() == wx.ID_OK:
  36. print "SimpleDialog OK"
  37. else:
  38. print "SimpleDialog Cancel"
  39. dlg1.Destroy()
  40.  
  41. dlg2 = BitmapDialog("BitmapDialog Text")
  42. if dlg2.ShowModal() == wx.ID_OK:
  43. print "BitmapDialog OK"
  44. else:
  45. print "BitmapDialog Cancel"
  46. dlg2.Destroy()
  47.  
  48. app.MainLoop()
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,542
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 171
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: wxpython button help

 
0
  #2
May 28th, 2009
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:
  1. class BitmapDialog(wx.Dialog):
  2. def __init__(self, text):
  3. wx.Dialog.__init__(self, None, -1, title="BitmapDialog",
  4. size=(250,150))
  5.  
  6. bmp = wx.Bitmap('ok.png')
  7. w, h = bmp.GetSize()
  8. x = 35
  9. y = 90
  10. wx.StaticBitmap(self, -1, bmp,
  11. pos=(x, y))
  12. okButton = wx.Button(self, wx.ID_OK, ' OK ',
  13. pos=(x+w, y), size=(w*2, h))
  14. okButton.SetDefault()
  15.  
  16. bmp = wx.Bitmap('cancel.png')
  17. w, h = bmp.GetSize()
  18. x = 135
  19. y = 90
  20. wx.StaticBitmap(self, -1, bmp,
  21. pos=(x, y))
  22. cancelButton = wx.Button(self, wx.ID_CANCEL, ' Cancel ',
  23. pos=(x+w, y), size=(w*2, h))
Actually, the way these buttons respond to keystrokes depends on the OS.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 2
Reputation: fkadri is an unknown quantity at this point 
Solved Threads: 0
fkadri fkadri is offline Offline
Newbie Poster

Re: wxpython button help

 
0
  #3
May 28th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC