這篇文章將為大家詳細(xì)講解有關(guān)Python中tkinter label如何更新,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
剛察網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營(yíng)維護(hù)。成都創(chuàng)新互聯(lián)公司于2013年開始到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)公司。例子_1:
代碼_1:
from tkinter import Tk, Checkbutton, Label from tkinter import StringVar, IntVar root = Tk() text = StringVar() text.set('old') status = IntVar() def change(): if status.get() == 1: # if clicked text.set('new') else: text.set('old') cb = Checkbutton(root, variable=status, command=change) lb = Label(root, textvariable=text) cb.pack() lb.pack() root.mainloop()
例子_2:
代碼_2:
from tkinter import * import time class StopWatch(Frame): '''實(shí)現(xiàn)一個(gè)秒表部件''' msec = 50 def __init__(self, parent=None, **kw): Frame.__init__(self, parent, kw) self._start = 0.0 self._elapsedtime = 0.0 self._running = False self.timestr = StringVar() self.makeWidgets() self.flag = True def makeWidgets(self): '''制作時(shí)間標(biāo)簽''' l = Label(self, textvariable = self.timestr) self._setTime(self._elapsedtime) l.pack(fill = X, expand = NO, pady = 2, padx = 2) def _update(self): self._elapsedtime = time.time() - self._start self._setTime(self._elapsedtime) self._timer = self.after(self.msec, self._update) def _setTime(self, elap): '''將時(shí)間格式改為 分:秒:百分秒''' minutes = int(elap/60) seconds = int(elap-minutes*60.0) hseconds = int((elap - minutes*60.0 - seconds) *100) self.timestr.set('%2d:%2d:%2d' %(minutes, seconds, hseconds)) def Start(self): if not self._running: self._start = time.time() - self._elapsedtime self._update() self._running = True def Stop(self): '''停止秒表''' if self._running: self.after_cancel(self._timer) self._elapsedtime = time.time() - self._start self._setTime(self._elapsedtime) self._running = False def Reset(self): '''重設(shè)秒表''' self._start = time.time() self._elapsedtime = 0.0 self._setTime(self._elapsedtime) def stopwatch(self): if self.flag == True: self.pack(side = TOP) Button(self, text = 'start', command = self.Start).pack(side = LEFT) Button(self, text = 'stop', command = self.Stop).pack(side = LEFT) Button(self, text = 'reset', command = self.Reset).pack(side = LEFT) Button(self, text = 'quit', command = self.quit).pack(side = LEFT) self.flag = False class Watch(Frame): msec = 1000 def __init__(self, parent=None, **kw): Frame.__init__(self, parent, kw) self._running = False self.timestr1 = StringVar() self.timestr2 = StringVar() self.makeWidgets() self.flag = True def makeWidgets(self): l1 = Label(self, textvariable = self.timestr1) l2 = Label(self, textvariable = self.timestr2) l1.pack() l2.pack() def _update(self): self._settime() self.timer = self.after(self.msec, self._update) def _settime(self): today1 = str(time.strftime('%Y-%m-%d', time.localtime(time.time()))) time1 = str(time.strftime('%H:%M:%S', time.localtime(time.time()))) self.timestr1.set(today1) self.timestr2.set(time1) def start(self): self._update() self.pack(side = TOP) if __name__ == '__main__': def main(): root = Tk() root.geometry('250x150') frame1 = Frame(root) frame1.pack(side = BOTTOM) sw = StopWatch(root) stpwtch = Button(frame1, text = '秒表', command = sw.stopwatch) stpwtch.pack(side = RIGHT) mw = Watch(root) mywatch = Button(frame1, text = '時(shí)鐘', command = mw.start) mywatch.pack(side = LEFT) root.mainloop() main()
關(guān)于“Python中tkinter label如何更新”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)站題目:Python中tkinterlabel如何更新-創(chuàng)新互聯(lián)
標(biāo)題路徑:http://jinyejixie.com/article24/decoje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、軟件開發(fā)、網(wǎng)站設(shè)計(jì)公司、移動(dòng)網(wǎng)站建設(shè)、ChatGPT、靜態(tài)網(wǎng)站
聲明:本網(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í)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容