wxpython 3.6 创建自定义事件(点击计数) CustomEventFrame

#!/usr/bin/env python
#-*-coding:utf-8 -*-

import wx

class TwoButtonEvent(wx.PyCommandEvent):
    def __init__(self,evtType,id):
        wx.PyCommandEvent.__init__(self,evtType,id)
        self.clickCount=0
    def GetClickCount(self):
        return self.clickCount
    def SetClickCount(self,count):
        self.clickCount=count
        
myEVT_TWO_BUTTON=wx.NewEventType()
EVT_TWO_BUTTON=wx.PyEventBinder(myEVT_TWO_BUTTON,1)

class TwoButtonPanel(wx.Panel):
    def __init__(self,parent,id=1,leftText="Left",
                 rightText="Right"):
        wx.Panel.__init__(self,parent,id)
        self.leftButton=wx.Button(self,label=leftText)
        self.rightButton=wx.Button(self,label=rightText,
                                   pos=(100,0))
        self.leftClick=False
        self.rightClick=False
        self.clickCount=0
        
        self.leftButton.Bind(wx.EVT_LEFT_DOWN,self.OnLeftClick)
        self.rightButton.Bind(wx.EVT_LEFT_DOWN,self.OnRightClick)
        
    def OnLeftClick(self,event):
        self.leftClick=True
        self.OnClick()
        event.Skip()
        
    def OnRightClick(self,event):
        self.rightClick=True
        self.OnClick()
        event.Skip()
        
    def OnClick(self):
        self.clickCount+=1
        if self.leftClick and self.rightClick:
            self.leftClick=False
            self.rightClick=False
            evt=TwoButtonEvent(myEVT_TWO_BUTTON,self.GetId())
            evt.SetClickCount(self.clickCount)
            self.GetEventHandler().ProcessEvent(evt)
            
class CustomEventFrame(wx.Frame):
    def  __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Click Count:0',
                          size=(300,100))
        panel=TwoButtonPanel(self)
        self.Bind(EVT_TWO_BUTTON,self.OnTwoClick,panel)
        
    def OnTwoClick(self,event):
        self.SetTitle("Click Count:%s"%event.GetClickCount())
        
if __name__=='__main__':
    app=wx.PySimpleApp()
    frame=CustomEventFrame(parent=None,id=-1)
    frame.Show()
    app.MainLoop()
    

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值