界面不能看,不想改,先保存下
#coding=utf-8
'''
Created on 2014-3-26
Stopwatch: The Game
'''
__metaclass__ = type
import wx
class GameFrame(wx.Frame):
def __init__(self,title):
super(GameFrame,self).__init__(None,title=title, pos=(250,200), size=(550,400))
#参数们
self.time = 0
self.attempts = 0
self.success = 0
#创建定时器
self.my_timer = wx.Timer(self)
#绑定定时器my_timer,与方法tick
self.Bind(wx.EVT_TIMER, self.OnTimer,self.my_timer)
self.my_timer.Stop()
pnl = wx.Panel(self) #最大的panel
hbox = wx.BoxSizer(wx.HORIZONTAL) #整个panel的左右布局
panel_btn = wx.Panel(pnl) #放三个按钮和两个计数的panel
box_btn = wx.BoxSizer(wx.VERTICAL) #三个按钮和两个计数按垂直布局
btn_start = wx.Button(panel_btn, label = "start")
btn_start.Bind(wx.EVT_BUTTON,self.func_start)
box_btn.Add(btn_start,0,wx.TOP|wx.BOTTOM,5)
btn_stop = wx.Button(panel_btn, label = "stop")
btn_stop.Bind(wx.EVT_BUTTON,self.func_stop)
box_btn.Add(btn_stop,0,wx.TOP|wx.BOTTOM,5)
btn_restart = wx.Button(panel_btn, label = "restart")
btn_restart.Bind(wx.EVT_BUTTON,self.func_restart)
box_btn.Add(btn_restart,0,wx.TOP|wx.BOTTOM,5)
self.stsu = wx.StaticText(panel_btn,label='success : 0')
box_btn.Add(self.stsu,0,wx.TOP,50)
self.stat = wx.StaticText(panel_btn,label='attempts : 0')
box_btn.Add(self.stat,0,wx.TOP,5)
panel_btn.SetSizer(box_btn) #设置按钮panel的sizer
panel_btn.Layout()
panel2 = wx.Panel(pnl)
box2 = wx.BoxSizer(wx.VERTICAL)
self.stext = wx.StaticText(panel2,-1,"文字",style = wx.ALIGN_CENTER)
self.stext.SetForegroundColour("White")
self.stext.SetBackgroundColour("Black")
font = wx.Font(30, wx.SWISS, wx.NORMAL, wx.BOLD)
self.stext.SetFont(font)
#stext.SetSize( stext.GetBestSize() )
box2.Add(self.stext,1,wx.EXPAND|wx.ALL|wx.ALIGN_CENTER,30)
panel2.SetSizer(box2)
panel2.Layout()
hbox.Add(panel_btn,0,wx.ALL,60)
hbox.Add(panel2,1,wx.EXPAND)
pnl.SetSizer(hbox)
pnl.Layout()
#每0.1s time增加1
def OnTimer(self,event):
self.time += 1
self.draw()
#单击start按钮要执行的
def func_start(self,event):
if not self.my_timer.IsRunning():
self.my_timer.Start(100)
def func_stop(self,event):
if self.my_timer.IsRunning():
self.my_timer.Stop()
self.attempts += 1
if not 1<(self.time % 50)<49 :
self.success += 1
self.draw()
def func_restart(self,event):
self.my_timer.Stop()
self.time = 0
self.attempts = 0
self.success = 0
self.draw()
#更新 时间、success、attempts
def draw(self):
self.stext.SetLabel( self.format(self.time))
self.stsu.SetLabel("success : %s" % self.success )
self.stat.SetLabel("attempts : %s" % self.attempts )
#time转化为想要的输出格式
def format(self,time):
mins = self.time / 600
sec = (self.time - 600 * mins) / 10
tenth = self.time % 10
return "%d:%.2d.%d" % (mins,sec,tenth)
if __name__ == "__main__":
app = wx.App(redirect=True)
top = GameFrame(title = "Stopwatch")
top.Show()
app.MainLoop()