I have formed a collection of wxGenToggleButtons to act like radio buttons.
I do this by setting all the buttons to the up position when one is clicked
and then depress the one that is actually clicked. A button would bounce up
if clicked when in the down position with the new code. The button should
stay down. I believe the problem is with the order in which the actions
were being performed.
Code is below if you are interested.
Regards,
Gordon Williams
'''
genButtonBox.py
Showing the use of wxGenButton, wxGenBitmapButton, wxGenToggleButton
and wxGenBitmapToggleButton which are in buttons.py.
This example shows how a general button box can be made to handle
normal push buttons, radio buttons and check buttons with text labels and
bitmaps.
The radio and check buttons are in a button format and indicate
their active state by staying depressed.
The wxGenButton is the base. It acts like a normal button but you
are able to better control how it looks, bevel width, colours, etc.
wxGenBitmapButton
is a button with one or more bitmaps that show the various states the button
can be
in.
wxGenToggleButton stays depressed when clicked, until clicked again.
wxGenBitmapToggleButton the same but with bitmaps.
genButtonBox allows you to quickly make an grid of uniformly sized buttons
that act as normal push buttons, radio buttons or check buttons within a
group.
Each button can have its own event handler, or none if desired.
USAGE:
Make a list of directories. Each list forms a group for the radio buttons,
therefore
only one button within the group will be active. Each directory within the
list defines
the parameters for the button.
Example:
bGroup = [
{'label' : 'Check 5', #Text label
'onEvent' : self.myCheckEvent, #Your event handler
(optional)
'default': TRUE}, #Show the button initially
on (optional)
{'label' : 'Check 6',
'onEvent' : None}, #Could leave this out
{'label' : 'Check 7',
'onEvent' : self.myCheckEvent},
{'label' : 'Check 8',
'onEvent' : self.myCheckEvent,
'default': TRUE},
]
Then to make the box
g= genButtonBox(parent, Id, bGroup, type, row, col, hgap, vgap,
style, bezel, focus)
where:
parent is parent window
Id is panel Id or -1 for default
bGroup is the list of button definitions
type is one of "push", "radio", "check", "bmppush",
"bmpradio", "bmpcheck"
row is number of rows in grid
col is number of columns in grid
hgap is horizontal gap between buttons
vgap is vertical gap between buttons
style is the border around the group (see wxWindow for
styles)
bezel is bezel size
focus (TRUE, FALSE) whether the focus line should be shown
Use as you wish
Gordon Williams, 99/12/17
'''
from wxPython.wx import *
from wxPython.lib.grids import wxGridSizer
from wxPython.lib.buttons import *
class genButtonBox(wxPanel):
def __init__(self, parent, Id, elements, type= "push",
row=0, col=0, hgap=0, vgap=0,
style= wxDOUBLE_BORDER| wxTAB_TRAVERSAL, bezel=2,
focus= FALSE):
wxPanel.__init__(self, parent, Id, style= style)
gs = wxGridSizer(row, col, hgap, vgap) # rows, cols, hgap, vgap
self.group= #group of button instances in each ButtonBox
for i in range(len(elements)):
e = elements[i]
label = e['label'] # text label
bmp = e.get('bmp', None) # bitmap object
self.onEventName = e.get('onEvent', None) # the event handler
for the button
defaulton= e.get('default', FALSE) # if default is not defined
set to false
b = self.addButton(label, bmp, type, defaulton, bezel, focus,
self.onEventName)
self.group.append(b)
gs.Add(b, 0, wxEXPAND)
self.SetAutoLayout(true)
self.SetSizer(gs)
self.Fit() # Im snot sure if this is required
def addButton(self, label, bmp, type, defaulton, bezel, focus,
onEventName):
butId= wxNewId()
if type == "push":
b= wxGenButton(self, butId, label)
EVT_BUTTON(self, butId, onEventName)
elif type == "radio":
b= wxGenToggleButton(self, butId, label)
EVT_BUTTON(self, butId, self.radioEvent)
b.SetToggle(defaulton)
elif type == "check":
b= wxGenToggleButton(self, butId, label)
EVT_BUTTON(self, butId, onEventName)
b.SetToggle(defaulton)
elif type == "bmppush":
b= wxGenBitmapButton(self, butId, bmp)
EVT_BUTTON(self, butId, onEventName)
b.SetLabel(label)
elif type == "bmpradio":
b= wxGenBitmapToggleButton(self, butId, bmp)
EVT_BUTTON(self, butId, self.radioEvent)
b.SetToggle(defaulton)
b.SetLabel(label)
elif type == "bmpcheck":
b= wxGenBitmapToggleButton(self, butId, bmp)
EVT_BUTTON(self, butId, onEventName)
b.SetToggle(defaulton)
b.SetLabel(label)
b.SetBezelWidth(bezel)
b.SetBestSize()
b.SetUseFocusIndicator(focus)
return b
def radioEvent(self, event):
for item in self.group:
if item.GetToggle() == TRUE: #all put up
item.SetToggle(FALSE) #put in up position
item.Refresh()
event.GetButtonObj() .SetToggle(TRUE) #clicked button down
event.SetIsDown(TRUE)
self.onEventName(event)
# ------------------------ Test ----------------------------------
if __name__ == '__main__':
class myFrame(wxFrame):
def __init__(self, parent, Id, title, pos, size):
wxFrame.__init__(self, parent, Id, title, pos, size)
self.Show(TRUE)
class myApp(wxApp):
def OnInit(self):
f= myFrame(NULL, -1, 'wxGenButton Tester',
wxPoint(100, 100), wxSize(300, 500))
f.SetBackgroundColour(wxNamedColour("LIGHT GREY"))
self.SetTopWindow(f)
# Buttons with labels *************************
elements = [
{'label' : 'Choice 1',
'onEvent' : self.myPushEvent},
{'label' : 'Choice 2',
'onEvent' : self.myPushEvent},
]
e1 = [
{'label' : 'Radio 3',
'onEvent' : self.myRadioEvent,
'default': TRUE},
{'label' : 'Radio 4',
'onEvent' : self.myRadioEvent},
]
e2 = [
{'label' : 'Check 5',
'onEvent' : self.myCheckEvent,
'default': TRUE},
{'label' : 'Check 6',
'onEvent' : self.myCheckEvent},
{'label' : 'Check 7',
'onEvent' : self.myCheckEvent},
{'label' : 'Check 8',
'onEvent' : self.myCheckEvent,
'default': TRUE},
]
g= genButtonBox(f, -1, elements, "push", 2, 1, 0, 0,
style= wxSTATIC_BORDER, bezel=2, focus= FALSE)
h= genButtonBox(f, -1, e1, "radio", 1, 2, 2, 2,
style= wxDOUBLE_BORDER| wxTAB_TRAVERSAL, bezel=4, focus=
FALSE)
i= genButtonBox(f, -1, e2, "check", 2, 2, 4, 4,
style= wxDOUBLE_BORDER| wxTAB_TRAVERSAL, bezel=2, focus=
FALSE)
a= wxStaticText(f, -1, "Normal Push Buttons (Bezel=2, Gaps= 0,
style= Static)")
b= wxStaticText(f, -1, "Radio Buttons (Bezel=4, Gaps= 2, style=
Double)")
c= wxStaticText(f, -1, "Check Buttons (Bezel=2, Gaps= 4, style=
Double)")
# Buttons with icons *************************************
bmp1 = wxBitmap('bitmaps/paste.bmp', wxBITMAP_TYPE_BMP)
bmp2 = wxBitmap('bitmaps/open.bmp', wxBITMAP_TYPE_BMP)
bmp3 = wxBitmap('bitmaps/new.bmp', wxBITMAP_TYPE_BMP)
bmp4 = wxBitmap('bitmaps/smiles.bmp', wxBITMAP_TYPE_BMP)
bmpel = [
{'label' : 'Bitmap paste',
'bmp' : bmp1,
'onEvent' : self.myCheckEvent},
{'label' : 'Bitmap open',
'bmp' : bmp2,
'onEvent' : self.myCheckEvent},
{'label' : 'Bitmap new',
'bmp' : bmp3,
'onEvent' : self.myCheckEvent},
{'label' : 'Bitmap smile',
'bmp' : bmp4,
'onEvent' : None}, #no event handler for this one
]
q= wxStaticText(f, -1, "Check Buttons (Bezel=2, Gaps= 0, style=
Static)")
r= genButtonBox(f, -1, bmpel, "bmpcheck", 2, 2, 0, 0,
style= wxSTATIC_BORDER, bezel=2, focus= FALSE)
# Now put them in a sizer ******************************
box = wxBoxSizer(wxVERTICAL)
box.Add(a, 1, wxEXPAND)
box.Add(g, 1, wxEXPAND)
box.Add(b, 1, wxEXPAND)
box.Add(h, 1, wxEXPAND)
box.Add(c, 1, wxEXPAND)
box.Add(i, 1, wxEXPAND)
box.Add(q, 1, wxEXPAND)
box.Add(r, 1, wxEXPAND)
f.SetAutoLayout(true)
f.SetSizer(box)
return(TRUE)
# Events specified above for each button *******************
def myPushEvent(self, event):
change= ('Button now up. ', 'Button now down. ')
print 'Push', change[event.isDown], 'Label is',
event.GetButtonObj().GetLabel()
def myRadioEvent(self, event):
change= ('Button now up. ', 'Button now down. ')
print 'Radio', change[event.isDown], 'Label is',
event.GetButtonObj().GetLabel()
def myCheckEvent(self, event):
change= ('Button now up. ', 'Button now down. ')
print 'Check', change[event.isDown], 'Label is',
event.GetButtonObj().GetLabel()
app = myApp(0)
app.MainLoop()
ยทยทยท
----- Original Message -----
From: "Gordon Williams" <g_will@cyberus.ca>
To: "wxPython List" <wxpython-users@lists.wxwindows.org>
Sent: Friday, November 30, 2001 10:19 PM
Subject: Patch for buttons.py
RCS-ID: $Id: buttons.py,v 1.5 2001/02/16 08:19:44 robind Exp $
I found that when I got version 1.5 of buttons.py in wxPython 2.3.0, some
of
my button operation based on wxGenToggleButton was not the same as it was
with earlier versions.I have made 3 changes to buttons.py to get it working as before:
1) Deleted all the self.evtToSend
2) Deleted the OnIdle Event handler and method
3) Added self.GetEventHandler().ProcessEvent(evt) to Notify