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