這篇文章主要介紹“怎么用Python實現(xiàn)簡單的計時器”,在日常操作中,相信很多人在怎么用Python實現(xiàn)簡單的計時器問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么用Python實現(xiàn)簡單的計時器”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
創(chuàng)新互聯(lián)建站專注于同德網(wǎng)站建設服務及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供同德營銷型網(wǎng)站建設,同德網(wǎng)站制作、同德網(wǎng)頁設計、同德網(wǎng)站官網(wǎng)定制、微信小程序定制開發(fā)服務,打造同德網(wǎng)絡公司原創(chuàng)品牌,更為您提供同德網(wǎng)站排名全網(wǎng)營銷落地服務。
開發(fā)工具
Python版本:3.6.4
相關模塊:
SimpleGUICS2Pygame模塊。
環(huán)境搭建
安裝Python并添加到環(huán)境變量,pip安裝需要的相關模塊即可。
原理簡介
1、首先創(chuàng)建主界面和一個計時器。
def main(): global t, color t = 0 color = 'white' frame = simplegui.create_frame('Timer', 200, 200, 150) # 1000 / 100 = 10, 即t自加10次為一秒 global timer timer = simplegui.create_timer(100, timerHandler) frame.set_draw_handler(drawHandler) button_start = frame.add_button('Start', Start, 150) button_stop = frame.add_button('Stop', Stop, 150) button_clear = frame.add_button('Clear', Clear, 150) frame.start() if __name__ == '__main__': main()
2、t每計數(shù)十次為一秒,因此將t轉(zhuǎn)換為分秒格式的代碼。
''' Function: 將時間轉(zhuǎn)為<A:BC.D>格式 ''' def Convert(t): D = t % 10 # 十位 B = (t // 100) % 6 # 個位 C = (t // 10) % 10 # 分鐘 A = t // 600 return str(A) + ':' + str(B) + str(C) + '.' + str(D)
3、實現(xiàn)開始計時,結(jié)束計時,清空當前計時和將計時繪制在主界面上了。
Function: 開始計時 ''' def Start(): global timer, color color = 'white' if not timer.is_running(): timer.start() ''' Function: 停止計時 ''' def Stop(): global timer, color timer.stop() color = 'red' ''' Function: 清空 ''' def Clear(): global t, timer, color timer.stop() t = 0 color = 'white' ''' Function: 計時器 ''' def timerHandler(): global t t += 1 ''' Function: 繪制時間 ''' def drawHandler(canvas): t_convert = Convert(t) canvas.draw_text(t_convert, (25, 120), 60, color, 'serif')
到此,關于“怎么用Python實現(xiàn)簡單的計時器”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
分享標題:怎么用Python實現(xiàn)簡單的計時器
網(wǎng)頁鏈接:http://aaarwkj.com/article4/pdpgoe.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設計、App開發(fā)、軟件開發(fā)、響應式網(wǎng)站、小程序開發(fā)、網(wǎng)站設計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)