使用下面的代码片段,它会生成如下内容:
...每当用户在应用程序框架窗口中用鼠标任意位置点击时,我希望蓝色(子类别Panel)小部件变为红色。就像目前一样,它仅在单击窗口小部件时才会做出反应 - 但它不会对按钮或框架中其他位置的点击做出反应。
所以我的问题是:
(只是更改父级,例如self.parent.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents)
根本不起作用;我也找到wx.lib.evtmgr,但我找不到正确的语法来设置它以便它可以工作)。
代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# generated by wxGlade 0.6.3 on Mon Jan 12 21:20:22 2015
import wx
# begin wxGlade: extracode
# end wxGlade
class MyPanel(wx.Panel):
def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0, name="MyPanel"):
super(MyPanel, self).__init__(parent, id, pos, size, style, name)
self.parent = parent
self.lmb = False
self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents) # only reacts on its own clicks, not globally!
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnMouseEvents(self, event):
print("OnMouseEvents")
self.lmb = event.LeftDown() or event.LeftIsDown()
self.Refresh(False)
def OnPaint(self, event):
w, h = self.GetSize()
if self.lmb: mybrush = wx.Brush(wx.NamedColour("red"))
else: mybrush = wx.Brush(wx.NamedColour("blue"))
dc = wx.PaintDC(self)
dc.SetPen(wx.TRANSPARENT_PEN)
dc.SetBrush(mybrush)
dc.DrawRectangle(0, 0, w, h)
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: MyFrame.__init__
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.button_1 = wx.Button(self, -1, "button_1")
self.panel_1 = MyPanel(self, -1)
self.__set_properties()
self.__do_layout()
# end wxGlade
def __set_properties(self):
# begin wxGlade: MyFrame.__set_properties
self.SetTitle("frame_1")
self.SetSize((200, 200))
# end wxGlade
def __do_layout(self):
# begin wxGlade: MyFrame.__do_layout
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_1.Add(self.button_1, 0, 0, 0)
sizer_1.Add(self.panel_1, 1, wx.EXPAND, 0)
self.SetSizer(sizer_1)
self.Layout()
# end wxGlade
# end of class MyFrame
if __name__ == "__main__":
app = wx.PySimpleApp(0)
wx.InitAllImageHandlers()
frame_1 = MyFrame(None, -1, "")
app.SetTopWindow(frame_1)
frame_1.Show()
app.MainLoop()
答案 0 :(得分:1)
好的,我想我得上班了;但请注意:
考虑到所有这些,解决方案是:
event.Skip()
结尾,以便将事件传播到原始按钮等,因此其原始点击处理程序也可以正常工作。有了这个,现在用下面的代码我得到了这个:
请注意,屏幕截图上鼠标所在的区域是"覆盖"通过sizer,但是sizer不处理点击 - 所以基本上它是框架本身响应那里的点击(将监视方块变为红色)。所以基本上框架窗口表面上的每个小部件都处理它自己的区域;并且未覆盖的任何东西 - 即使该区域中存在分级器 - 实际上是由框架处理的。
代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# generated by wxGlade 0.6.3 on Mon Jan 12 21:20:22 2015
import wx
# begin wxGlade: extracode
# end wxGlade
class MyPanel(wx.Panel):
def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0, name="MyPanel"):
super(MyPanel, self).__init__(parent, id, pos, size, style, name)
self.lmb = False
#self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents) # only reacts on its own clicks, not globally! # use RecursiveSetListeners now; should be called from parent class after all init layout is done
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnMouseEvents(self, event):
print("OnMouseEvents")
self.lmb = event.LeftDown() or event.LeftIsDown()
self.Refresh(False)
event.Skip() # propagate, in case button is clicked
def OnPaint(self, event):
w, h = self.GetSize()
if self.lmb: mybrush = wx.Brush(wx.NamedColour("red"))
else: mybrush = wx.Brush(wx.NamedColour("blue"))
dc = wx.PaintDC(self)
dc.SetPen(wx.TRANSPARENT_PEN)
dc.SetBrush(mybrush)
dc.DrawRectangle(0, 0, w, h)
def RecursiveSetListeners(self, inwidget):
print("setting listeners: " + inwidget.GetName())
inwidget.Connect(wx.ID_ANY, -1, wx.wxEVT_LEFT_DOWN, self.OnMouseEvents)
inwidget.Connect(wx.ID_ANY, -1, wx.wxEVT_LEFT_UP, self.OnMouseEvents)
for child in inwidget.GetChildren():
self.RecursiveSetListeners(child)
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: MyFrame.__init__
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.button_1 = wx.Button(self, -1, "button_1", name="button_1")
self.panel_1 = MyPanel(self, -1, name="panel_1")
self.button_2 = wx.Button(self.panel_1, -1, "button_2", name="button_1")
#self.Connect(wx.ID_ANY, -1, wx.EVT_MOUSE_EVENTS, self.panel_1.OnMouseEvents) # 'wx.wxEVT_MOUSE_EVENTS: AttributeError: 'module' has no attribute 'wxEVT_MOUSE_EVENTS';; wx.EVT_MOUSE_EVENTS: TypeError: in method 'EvtHandler_Connect', expected argument 4 of type 'int'
# these work:
#self.Connect(wx.ID_ANY, -1, wx.wxEVT_LEFT_DOWN, self.panel_1.OnMouseEvents)
#self.Connect(wx.ID_ANY, -1, wx.wxEVT_LEFT_UP, self.panel_1.OnMouseEvents)
#self.button_1.Connect(wx.ID_ANY, -1, wx.wxEVT_LEFT_DOWN, self.panel_1.OnMouseEvents)
#self.button_1.Connect(wx.ID_ANY, -1, wx.wxEVT_LEFT_UP, self.panel_1.OnMouseEvents)
self.__set_properties()
self.__do_layout()
print(self.GetChildren())
self.panel_1.RecursiveSetListeners(self)
# end wxGlade
def __set_properties(self):
# begin wxGlade: MyFrame.__set_properties
self.SetTitle("frame_1")
self.SetSize((200, 200))
# end wxGlade
def __do_layout(self):
# begin wxGlade: MyFrame.__do_layout
sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
sizer_2.Add(self.button_2, 0,0,0)
self.panel_1.SetSizer(sizer_2)
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_1.Add(self.button_1, 0, 0, 0)
sizer_1.Add(self.panel_1, 1, wx.EXPAND, 0)
self.SetSizer(sizer_1)
self.Layout()
# end wxGlade
# end of class MyFrame
if __name__ == "__main__":
app = wx.PySimpleApp(0)
wx.InitAllImageHandlers()
frame_1 = MyFrame(None, -1, "")
app.SetTopWindow(frame_1)
frame_1.Show()
app.MainLoop()