I have a program where I want to catch all of the mouse events of a panels children. To do this I have been using the following code.
self.panel.Bind(wx.EVT_LEFT_DOWN, self.onLeftClick)
self.panel.Bind(wx.EVT_MOTION, self.onMouseMove)
map(lambda child: wx.EVT_LEFT_DOWN(child, self.onLeftClick), self.panel.GetChildren())
map(lambda child: wx.EVT_MOTION(child, self.onMouseMove), self.panel.GetChildren())
This seems to work for the wx.Button and the wx.TextCtrl but not the wx.MediaCtrl. I have included the full text of my code below. Any help on this would be appreciated,
this is to test the usefullness of gridbagsizer and moving items within it
import wx
import wx.media
class myFrame(wx.Frame):
def init(self, parent, mytitle, mysize):
wx.Frame.init(self, parent, wx.ID_ANY, mytitle, size=mysize)
self.panel = wx.Panel(self, -1, size=mysize)
self.sizer = wx.GridBagSizer(0, 0)
self.sizer.SetDimension(0, 0, mysize[0], mysize[1])
#creat media player display panel
try:
[self.mc](http://self.mc) = wx.media.MediaCtrl(self.panel, style=wx.SIMPLE_BORDER, szBackend=wx.media.MEDIABACKEND_WMP10, id=3224, size=(400, 400))
except NotImplementedError:
self.Destroy()
raise
#creat test button and txtField
self.testButton = wx.Button(self.panel, 2522, "the test button", size = (100,40))
self.debugTxt = wx.TextCtrl(self.panel, -1, size=(150, 40))
#Add elements to the sizer
baseCell = self.sizer.GetEmptyCellSize()
self.sizer.Add([self.mc](http://self.mc), (1,1), wx.GBSpan(20, 40), flag=wx.EXPAND)
self.sizer.Add(self.testButton, (1, 50), wx.GBSpan(2, 10))
self.sizer.Add(self.debugTxt, (4, 50), wx.GBSpan(2, 10))
#Set up event bindings
self.panel.Bind(wx.EVT_LEFT_DOWN, self.onLeftClick)
self.panel.Bind(wx.EVT_MOTION, self.onMouseMove)
map(lambda child: wx.EVT_LEFT_DOWN(child, self.onLeftClick), self.panel.GetChildren())
map(lambda child: wx.EVT_MOTION(child, self.onMouseMove), self.panel.GetChildren())
self.panel.SetSizer(self.sizer)
self.panel.Layout()
self.changingObject = None
self.clickAction = 0
def onMouseMove(self, evt):
self.debugTxt.Clear()
# determine if the mouse is over an item and change the mouse cursur accordingly
if evt.GetEventObject().GetId() == self.panel.GetId():
thePosition = (0,0)
theSize = evt.GetEventObject().GetSize()
widthPercent = theSize.GetWidth()/10
heightPercent = theSize.GetHeight()/10
else:
thePosition = evt.GetEventObject().GetPosition()
theSize = evt.GetEventObject().GetSize()
widthPercent = theSize.GetWidth()/5
heightPercent = theSize.GetHeight()/5
if (evt.GetX() > theSize.GetWidth() - widthPercent) and (evt.GetY() > theSize.GetHeight() - heightPercent):
evt.GetEventObject().SetCursor(wx.StockCursor(wx.CURSOR_SIZENWSE))
else:
evt.GetEventObject().SetCursor(wx.StockCursor(wx.CURSOR_BULLSEYE))
self.debugTxt.AppendText("x: %d y: %d" % (evt.GetX()+thePosition[0], evt.GetY()+thePosition[1]))
# if a an item is clicked and dragged choose performe the correct click action
if self.changingObject != None and evt.Dragging():
if self.clickAction == 1:
self.expandItem(self.changingObject, self.sizer, evt.GetX()+thePosition[0], evt.GetY()+thePosition[1])
elif self.clickAction == 2:
self.setItemByPoint(self.changingObject, self.sizer, evt.GetX()+thePosition[0], evt.GetY()+thePosition[1])
# if dragging has stopped set changing object to none
if self.changingObject != None and not evt.Dragging():
self.changingObject = None
def onLeftClick(self, evt):
# Check if an item is being selected and set the clickAction so
# onMouseMove can select between moving or expanding the object.
if evt.GetEventObject().GetId() != self.panel.GetId():
self.changingObject = self.sizer.FindItem(evt.GetEventObject())
thePosition = evt.GetEventObject().GetPosition()
theSize = evt.GetEventObject().GetSize()
widthPercent = theSize.GetWidth()/5
heightPercent = theSize.GetHeight()/5
if (evt.GetX() > theSize.GetWidth() - widthPercent) and (evt.GetY() > theSize.GetHeight() - heightPercent):
self.clickAction = 1
else:
self.clickAction = 2
def setItemByPoint(self, item, theSizer, xPos, yPos):
baseCell = theSizer.GetEmptyCellSize()
destinationRowStart = yPos/baseCell.GetHeight()
destinationColStart = xPos/baseCell.GetWidth()
for i in theSizer.GetChildren():
startPos = i.GetPos()
endPos = i.GetEndPos()
if i.GetWindow().GetId() == item.GetWindow().GetId():
continue
if i.IntersectsPos(wx.GBPosition(destinationRowStart, destinationColStart), item.GetSpan()):
return
item.SetPos(wx.GBPosition(destinationRowStart, destinationColStart))
self.panel.Layout()
def expandItem(self, item, theSizer, xPos, yPos):
baseCell = theSizer.GetEmptyCellSize()
destinationRowStart = item.GetPos().GetRow()
destinationColStart = item.GetPos().GetCol()
rowSpan = item.GetSpan().GetRowspan()
colSpan = item.GetSpan().GetColspan()
oldXpos = item.GetWindow().GetPosition()[0]
oldYpos = item.GetWindow().GetPosition()[1]
width = item.GetWindow().GetSize().GetWidth()
height = item.GetWindow().GetSize().GetHeight()
if oldXpos > xPos:
self.debugTxt.AppendText(" crec")
destinationColStart = xPos/baseCell.GetWidth()
colSpan = item.GetEndPos().GetCol() - destinationColStart
width = width + (oldXpos - xPos)
else:
colSpan = (xPos - oldXpos)/baseCell.GetWidth()
width = xPos - oldXpos
if oldYpos > yPos:
self.debugTxt.AppendText(" rrec")
destinationRowStart = yPos/baseCell.GetHeight()
rowSpan = item.GetEndPos().GetRow() - destinationRowStart
height = height + (oldYpos - yPos)
else:
rowSpan = (yPos - oldYpos)/baseCell.GetHeight()
height = yPos - oldYpos
if colSpan <= 0:
return
if rowSpan <= 0:
return
for i in theSizer.GetChildren():
startPos = i.GetPos()
endPos = i.GetEndPos()
if i.GetWindow().GetId() == item.GetWindow().GetId():
continue
if i.IntersectsPos(wx.GBPosition(destinationRowStart, destinationColStart), wx.GBSpan(rowSpan, colSpan)):
return
self.debugTxt.AppendText(" h: %d w: %d" % (height, width))
item.SetSpan(wx.GBSpan(rowSpan, colSpan))
item.SetPos(wx.GBPosition(destinationRowStart, destinationColStart))
# The child window will reset itself to the initial size on a Layout call.
# So use SetInitialSize instead of setSize
item.GetWindow().SetInitialSize(wx.Size(baseCell.GetWidth() * item.GetSpan().GetColspan(), baseCell.GetHeight() * item.GetSpan().GetRowspan()))
self.panel.Layout()
app = wx.App(0)
mytitle = “Test GridBagSizer”
create a MyFrame instance and show the frame
myFrame(None, mytitle, wx.DisplaySize()).Show()
app.MainLoop()
···
–
“A government big enough to give us everything we want is a government big enough to take from us everything we have.”
-Gerald Ford