I'm trying to make a button that has 2 functions:
- left clicking - performs some function
- right clicking - alternately locks and unlocks the button
when the locked:
- left clicking is temporarlily disabled
- the button displays an "X" across its face
To make the "X" acroos the face when locked, I'm trying to get a wxCLientDC
and draw the "X"; but I'm having trouble. The "X" ends up getting drawn on
the button's parent window rather than on the button.
Am I doing something wrong or is there some other problem here?
I'm on linux box running RH9, using python 2.2. I see this problem with
wxPythonGTK-py2.2-2.4.0.7-1 and wxPythonGTK-py2.2-2.4.2.4-1.
Sample code below --------------------------------------------------------
from wxPython.wx import *
class LockButton( wxButton ):
def __init__( self, parent, *args, **kwargs ):
self.state = 1 # unlocked
wxButton.__init__( self, parent, *args, **kwargs )
EVT_LEFT_UP( self, self.OnLeftClick )
EVT_RIGHT_UP( self, self.OnRightClick )
def OnLeftClick(self, event):
if self.state:
event.Skip()
def OnRightClick(self, event): # lock / unlock
if self.state == 1:
self.lock( 0 )
else:
self.lock( 1 )
def lock( self, state ):
if state == 0: # Lock
self.state = 0
self.SetBackgroundColour( wxColor(255,0,0 ) )
w,h = self.GetSizeTuple()
dc = wxClientDC( self )
dc.BeginDrawing()
dc.SetPen( wxPen( (0,0,0), 3 ) )
dc.DrawLine( 0,0, w,h )
dc.DrawLine( w,0, 0,h )
dc.EndDrawing()
else: # Unlock
self.state = 1
self.SetBackgroundColour( wxColor(0,255,0) )
dc = wxClientDC( self )
dc.Clear()
···
##----------------------------------------------------------------------
if __name__ == '__main__':
class TestFrame(wxFrame):
def __init__(self, parent):
wxFrame.__init__(self, parent, -1, "Lock Button Test",
size=wxSize( 300, 200) )
panel = wxPanel( self, -1 )
button = LockButton( panel, id=50, label='',
pos=wxPoint(20, 50) )
EVT_BUTTON(button, 50, self.OnClick)
def OnClick(self, event):
print "Click! (%d)" % event.GetId()
class App(wxApp):
def __init__( self ):
wxApp.__init__(self, 0)
def OnInit( self ):
frame = TestFrame( None )
frame.Show(True)
self.SetTopWindow(frame)
return True
app = App()
app.MainLoop()