有一个wx Python小部件对整个窗口(全局)的鼠标点击做出反应吗?

时间:2015-01-12 20:34:09

标签: python wxpython wxwidgets

使用下面的代码片段,它会生成如下内容:

test.png

...每当用户在应用程序框架窗口中用鼠标任意位置点击时,我希望蓝色(子类别Panel)小部件变为红色。就像目前一样,它仅在单击窗口小部件时才会做出反应 - 但它不会对按钮或框架中其他位置的点击做出反应。

所以我的问题是:

  • 这个小部件是否正在聆听“全局” - 在wxPython中完全可以点击?
  • 如果是 - 我如何在下面的示例中执行此操作?

(只是更改父级,例如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()

1 个答案:

答案 0 :(得分:1)

好的,我想我得上班了;但请注意:

  • " wxMouseEvent不传播" wxWidgets Discussion Forum • View topic - Problem with mouse events
  • "各种消息来源称不可能(在实际的wxFrame元素中捕获鼠标事件)并且应该使用wxPanel来捕获事件" ; "一些鼠标事件进入框架,如wxEVT_ENTER(或其他任何调用)。其他人去小组,例如wxEVT_RIGHT_UP。您可以将这些事件转发到框架中。" ; "鼠标事件不会向上传播到窗口层次结构中,因此如果您的帧完全被其他窗口覆盖,那么它首先不会获取任何鼠标事件,因此您无法捕获它们有&#34。 Is it possible to catch mouse events in wxFrame
  • "每当你有一个wxFrame的子节点时,wxWidgets会自动假设你希望它覆盖wxFrame的整个区域。因此,框架的任何部分都不可见 - 因此没有任何事件可以使其成为您的处理程序。" Not catching all mouse events with wxWidgets

考虑到所有这些,解决方案是:

  • 以递归方式遍历框架中的所有(子)小部件;
  • 将他们的鼠标左下/上事件单独连接到单个事件处理程序;
  • 确保单个事件处理程序以event.Skip()结尾,以便将事件传播到原始按钮等,因此其原始点击处理程序也可以正常工作。

有了这个,现在用下面的代码我得到了这个:

test.png

请注意,屏幕截图上鼠标所在的区域是"覆盖"通过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()