【问题标题】:wxPython, how do I fire events?wxPython,我如何触发事件?
【发布时间】:2009-07-14 21:12:58
【问题描述】:

我正在制作自己的按钮类,这是我用 DC 绘制的面板的子类,当我的自定义按钮被按下时,我需要触发 wx.EVT_BUTTON。我该怎么做?

【问题讨论】:

    标签: python events wxpython


    【解决方案1】:

    Wiki 非常适合参考。 Andrea Gavana 有一个非常完整的配方来构建你自己的custom controls。以下内容直接取自那里并扩展了 FogleBird answered with(注意 self 指的是 wx.PyControl 的子类):

    def SendCheckBoxEvent(self):
        """ Actually sends the wx.wxEVT_COMMAND_CHECKBOX_CLICKED event. """
    
        # This part of the code may be reduced to a 3-liner code
        # but it is kept for better understanding the event handling.
        # If you can, however, avoid code duplication; in this case,
        # I could have done:
        #
        # self._checked = not self.IsChecked()
        # checkEvent = wx.CommandEvent(wx.wxEVT_COMMAND_CHECKBOX_CLICKED,
        #                              self.GetId())
        # checkEvent.SetInt(int(self._checked))
        if self.IsChecked():
    
            # We were checked, so we should become unchecked
            self._checked = False
    
            # Fire a wx.CommandEvent: this generates a
            # wx.wxEVT_COMMAND_CHECKBOX_CLICKED event that can be caught by the
            # developer by doing something like:
            # MyCheckBox.Bind(wx.EVT_CHECKBOX, self.OnCheckBox)
            checkEvent = wx.CommandEvent(wx.wxEVT_COMMAND_CHECKBOX_CLICKED,
                                         self.GetId())
    
            # Set the integer event value to 0 (we are switching to unchecked state)
            checkEvent.SetInt(0)
    
        else:
    
            # We were unchecked, so we should become checked
            self._checked = True
    
            checkEvent = wx.CommandEvent(wx.wxEVT_COMMAND_CHECKBOX_CLICKED,
                                         self.GetId())
    
            # Set the integer event value to 1 (we are switching to checked state)
            checkEvent.SetInt(1)
    
        # Set the originating object for the event (ourselves)
        checkEvent.SetEventObject(self)
    
        # Watch for a possible listener of this event that will catch it and
        # eventually process it
        self.GetEventHandler().ProcessEvent(checkEvent)
    
        # Refresh ourselves: the bitmap has changed
        self.Refresh()
    

    【讨论】:

      【解决方案2】:

      创建一个 wx.CommandEvent 对象,调用它的 setter 来设置适当的属性,并将它传递给 wx.PostEvent。

      http://docs.wxwidgets.org/stable/wx_wxcommandevent.html#wxcommandeventctor

      http://docs.wxwidgets.org/stable/wx_miscellany.html#wxpostevent

      这是重复的,这里有更多关于构造这些对象的信息:

      wxPython: Calling an event manually

      【讨论】:

      • 你能给我更详细的解释吗?实际上,我不过是个新手。另外,谢谢你的回答。
      猜你喜欢
      • 1970-01-01
      • 2017-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-23
      • 1970-01-01
      • 1970-01-01
      • 2021-06-26
      相关资源
      最近更新 更多