gpt4 book ai didi

python - wx Python 小部件是否对整个窗口(全局)的鼠标点击使用react?

转载 作者:太空宇宙 更新时间:2023-11-04 05:55:45 25 4
gpt4 key购买 nike

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

test.png

...我想让蓝色(子类面板)小部件变成红色,每次用户在应用程序框架窗口中用鼠标左键单击任何地方。目前,它仅在单击小部件本身时才会使用react - 但它不会对单击按钮或框架中的其他地方使用react。

所以我的问题是:

  • 在 wxPython 中是否可能实现这种 widget-listening-to-“全局”点击?
  • 如果是 - 在下面的示例中我该怎么做?

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

最佳答案

好的,我想我已经成功了;但请注意:

  • “wxMouseEvent 不传播”wxWidgets Discussion Forum • View topic - Problem with mouse events
  • “各种消息来源说这是不可能的(在实际的 wxFrame 元素中捕获鼠标事件)并且应该使用 wxPanels 来捕获事件”; “一些鼠标事件进入框架,如 wxEVT_ENTER(或任何名称)。其他进入面板,例如 wxEVT_RIGHT_UP。您可以将这些事件转发到框架。” ; “鼠标事件不会向上传播到窗口层次结构,因此如果您的框架完全被其他窗口覆盖,那么它首先不会收到任何鼠标事件,因此您无法在那里捕获它们。” 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 不处理点击 - 所以基本上是框架本身响应那里的点击(将监控方 block 变为红色)。所以基本上框架窗口表面上的每个小部件都处理自己的区域;并且任何未涵盖的内容 - 即使该区域中存在 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()

关于python - wx Python 小部件是否对整个窗口(全局)的鼠标点击使用react?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27910307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com