Python實現秒錶

2021-09-25 11:14:00 字數 3606 閱讀 5818

from tkinter import

*import time

# 繼承自 frame 的類

class

clock

(frame)

: def __init__

(self)

: frame.

__init__

(self)

self._start =

0.0 # 私有 開始時間設為 0

self._passtime =

0.0 # 已經過去了的時間設為 0

self._isrunning = false # 秒錶是否在執行 預設為否

self.timestr =

strin**ar

() # 時間字串

self.

layout

() # 圖形介面布局

# 布局

def layout

(self)

: lab =

label

(self, textvariable=self.timestr, font=

100)

self.

_settime

(self._passtime)

lab.

pack

(fill=

"x", expand=

1, pady=20)

# 設定時間

def _settime

(self, passtime)

: minutes =

int(passtime/60)

seconds =

int(passtime - minutes *

60.0

) mseconds =

int(

(passtime - minutes *

60.0

- seconds)*10

) self.timestr.

set(

'%02d:%02d.%01d'

%(minutes, seconds, mseconds)

) # 更新時間

def _update

(self)

: self._passtime = time.

time()

- self._start

self.

_settime

(self._passtime)

self.timer = self.

after

(100

, self._update) # 每 100ms 更新一次

# 開始

def begin

(self)

:if not self._isrunning:

self._start = time.

time()

- self._passtime

self.

_update()

self._isrunning = true

# 停止

def stop

(self)

:if self._isrunning:

self.

after_cancel

(self.timer)

self._passtime = time.

time()

- self._start

self.

_settime

(self._passtime)

self._isrunning = false

# 重設

def reset

(self)

:if not self._isrunning: # 設定只有在秒錶停止後 reset 才起作用

self._start = time.

time()

self._passtime =

0.0 self.

_settime

(self._passtime)

if __name__ ==

'__main__'

: def main()

:import tkinter

myclock =tk(

) clk =

clock()

width =

320 height =

96 screenwidth = myclock.

winfo_screenwidth()

screenheight = myclock.

winfo_screenheight()

alignstr =

'%dx%d+%d+%d'

%(width, height,

(screenwidth-width)/2

,(screenheight-height)/2

) myclock.

geometry

(alignstr)

myclock.

title

("my second chronograph"

) clk.

pack

(side=top)

b1 =

button

(myclock, text=

'start'

, command=clk.begin, bg=

"#82a6f5"

) b1.

pack

(fill=

"x", expand=

1, side=left)

b2 =

button

(myclock, text=

'stop'

, command=clk.stop, bg=

"#82a6f5"

) b2.

pack

(fill=

"x", expand=

1, side=left)

b3 =

button

(myclock, text=

'reset'

, command=clk.reset, bg=

"#82a6f5"

) b3.

pack

(fill=

"x", expand=

1, side=left)

b4 =

button

(myclock, text=

'quit'

, command=clk.quit, bg=

"#82a6f5"

) b4.

pack

(fill=

"x", expand=

1, side=left)

myclock.

mainloop()

main

()

python秒錶 Python 秒錶

最新專案 更多資訊。bistiming python的乙個日誌友好的秒錶和分析工具。當我們在網際網路上搜尋秒錶或計時模組中的python時,我們可以找到 2020 12 03已閱讀 n次 pip install withstopwatch from withstopwatch import stop...

Python 實現秒錶功能

python 實現秒錶功能 以下例項使用 time 模組來實現秒錶功能 例項 import time print 按下回車開始計時,按下 ctrl c 停止計時。while true try input 如果是 python 2.x 版本請使用 raw input starttime time.ti...

python 超級秒錶

設要記錄在沒有自動化的枯燥任務上花了多少時間。你沒有物理秒錶,要為筆記本或智慧型手機找到乙個免費的秒錶應用,沒有廣告,且不會將你的瀏覽歷史傳送給市場營銷人員,又出乎意料地困難 在你同意的許可協議中,它說它可以這樣做。你確實閱讀了許可協議,不是嗎?你可以自己用 python 寫乙個簡單的秒錶程式 總的...