python
import wx
class MainFrame(wx.Frame):
def __init__(self):
super().__init__(None, title='Continuous Action on Long Press', size=(200, 200))
self.panel = wx.Panel(self)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.counter = 0
self.label = wx.StaticText(self.panel, label=str(self.counter))
self.button = wx.Button(self.panel, label='Increase')
self.sizer.Add(self.label, 0, wx.ALL | wx.CENTER, 5)
self.sizer.Add(self.button, 0, wx.ALL | wx.CENTER, 5)
self.panel.SetSizerAndFit(self.sizer)
# 创建定时器
self.timer = wx.Timer(self)
# 绑定事件
self.Bind(wx.EVT_BUTTON, self.on_button_down, self.button)
self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)
self.Bind(wx.EVT_LEFT_UP, self.on_button_up) # 当鼠标释放时
self.Bind(wx.EVT_LEAVE_WINDOW, self.on_button_up) # 当鼠标离开按钮时
self.Centre()
def on_button_down(self, event):
# 开始定时器,每100毫秒触发on_timer事件
self.timer.Start(100)
def on_button_up(self, event):
# 停止定时器
self.timer.Stop()
def on_timer(self, event):
# 定时器事件,用于更新标签
self.counter += 1
self.label.SetLabel(str(self.counter))
def main():
app = wx.App(False)
frame = MainFrame()
frame.Show()
app.MainLoop()
if __name__ == "__main__":
main()