编写代码,通过wxpython实现一个计数的功能,点击开始计数开始,再点点击计数停止,再次点击计数开始
时间: 2023-03-29 15:01:56 浏览: 102
好的,我可以回答这个问题。以下是一个使用 wxpython 实现计数器的示例代码:
```python
import wx
class CounterFrame(wx.Frame):
def __init__(self):
super().__init__(None, title='计数器', size=(300, 200))
self.count =
self.timer = wx.Timer(self)
self.panel = wx.Panel(self)
self.start_button = wx.Button(self.panel, label='开始计数')
self.stop_button = wx.Button(self.panel, label='停止计数')
self.count_label = wx.StaticText(self.panel, label='', style=wx.ALIGN_CENTER)
self.Bind(wx.EVT_BUTTON, self.on_start, self.start_button)
self.Bind(wx.EVT_BUTTON, self.on_stop, self.stop_button)
self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.count_label, proportion=1, flag=wx.EXPAND)
sizer.Add(self.start_button, proportion=, flag=wx.EXPAND)
sizer.Add(self.stop_button, proportion=, flag=wx.EXPAND)
self.panel.SetSizer(sizer)
self.Center()
def on_start(self, event):
self.count =
self.count_label.SetLabel(str(self.count))
self.timer.Start(100)
def on_stop(self, event):
self.timer.Stop()
def on_timer(self, event):
self.count += 1
self.count_label.SetLabel(str(self.count))
if __name__ == '__main__':
app = wx.App()
frame = CounterFrame()
frame.Show()
app.MainLoop()
```
这个计数器界面包含一个开始计数按钮、一个停止计数按钮和一个显示计数的标签。点击开始计数按钮后,计数器会每秒钟加一,直到点击停止计数按钮为止。