【问题标题】:How can I detect a mouse click on a custom wx.Panel?如何检测自定义 wx.Panel 上的鼠标点击?
【发布时间】:2016-11-25 16:51:14
【问题描述】:

我在PythonwxPython 都是新手。无论如何,在按照官方教程解释如何做一个基本的文本编辑器之后,我决定继续写一个真正的文本编辑器。

现在,我的文本编辑器包含一个 MainWindow(继承自 wx.Frame),而 Notebook(继承自 wx.Notebook)又包含几个 标签(继承自wx.Panel的自定义类)。

如果我没有误会的话,wxPython 中的事件可以通过Bind() 函数检测并绑定到特定对象。

这是我的自定义面板类:

class TabContent(wx.Panel) :
    def __init__(self, parent) :
        # Calls the constructor for wx.Panel
        wx.Panel.__init__(self, parent = parent, id = wx.ID_ANY)

        # Creates a vertical sizer
        sizer = wx.BoxSizer(wx.VERTICAL)

        # Creates an empty multiline wx.TextCtrl
        textArea = wx.TextCtrl(self, style = wx.TE_MULTILINE)

        # Adds the text area to the sizer
        sizer.Add(textArea, 1, wx.EXPAND | wx.ALL, 2)

        # Sets the previously created sizer as this panel's sizer
        self.SetSizer(sizer)

        # Sets up events
        self.Bind(wx.EVT_RIGHT_DOWN, self.onMouseLeftClicked)

    def onMouseLeftClicked(self, event) :
        print("Left button of the mouse was clicked\n")

我希望能够检测选项卡本身的右键单击(例如,我可以打开一个菜单或打印一些东西来测试 wxPython 函数)。但是,用鼠标单击不会打印任何内容。知道为什么吗?

顺便说一句,我在 ArchLinux 上,使用 PyCharm Community Edition 2016.2.3、Python 3.5.2 和 wxpython 3.0.2。

【问题讨论】:

    标签: python-3.x event-handling wxpython mouseevent


    【解决方案1】:

    事件实际上已被捕获,但仅在选项卡的非常细的边框上。

    通过将事件处理程序放在Notebook 类中解决。

    【讨论】:

      猜你喜欢
      • 2011-11-21
      • 2021-04-15
      • 2020-11-22
      • 2018-03-17
      • 1970-01-01
      • 2020-04-18
      • 2023-03-12
      • 1970-01-01
      • 2021-09-06
      相关资源
      最近更新 更多