欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python+Tkinter+多線程的實(shí)例

 更新時(shí)間:2023年05月09日 16:00:12   作者:小威xiaowei2  
這篇文章主要介紹了python+Tkinter+多線程的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

python+Tkinter+多線程

界面和多線程一向是編程里比較難的地方,常見(jiàn)的做法一般是界面一個(gè)線程,后臺(tái)新開(kāi)一個(gè)工作線程,這兩個(gè)線程進(jìn)行通信,這樣可以讓界面不至于為響應(yīng)。

在python中可以利用隊(duì)列完成整體的架構(gòu)設(shè)計(jì)。

直接給大家看代碼吧,一個(gè)簡(jiǎn)單實(shí)例,非常好的例子。

import Tkinter,time,threading,random,Queue
class GuiPart():
    def __init__(self,master,queue,endCommand):
        self.queue=queue
        Tkinter.Button(master,text='Done',command=endCommand).pack()
    def processIncoming(self):
        while self.queue.qsize():
            try:
                msg=self.queue.get(0)
                print msg
            except Queue.Empty:
                pass
class ThreadedClient():
    def __init__(self,master):
        self.master=master
        self.queue=Queue.Queue()
        self.gui=GuiPart(master,self.queue,self.endApplication)
        self.running=True
        self.thread1=threading.Thread(target=self.workerThread1)
        self.thread1.start()
        self.periodicCall()
    def periodicCall(self):
        self.master.after(200,self.periodicCall)
        self.gui.processIncoming()
        if not self.running:
            self.master.destroy()
    def workerThread1(self):
        #self.ott=Tkinter.Tk()
        #self.ott.mainloop()
        while self.running:
            time.sleep(rand.random()*1.5)
            msg=rand.random() 
            self.queue.put(msg)
    def endApplication(self):
        self.running=False
rand=random.Random()
root=Tkinter.Tk()
client=ThreadedClient(root)
root.mainloop()

tkinter與多線程問(wèn)題

長(zhǎng)時(shí)間執(zhí)行后臺(tái)任務(wù),UI會(huì)處于無(wú)響應(yīng)狀態(tài)。在子線程里更新UI狀態(tài),聽(tīng)說(shuō)是不允許的。在哪個(gè)線程里調(diào)用了tk.mainloop(),就只能在哪個(gè)線程里更新UI。

下例演示了如何更新。

import Tkinter as tk
from ttk import *
import time
import Queue, threading
class MainWindow:
    def __init__(self):
        self.root = tk.Tk()
        self.root.title('Demo')
    def show(self):
        self.progress = tk.IntVar()
        self.progress_max = 100
        self.progressbar = Progressbar(self.root, mode='determinate', orient=tk.HORIZONTAL, variable=self.progress, maximum=self.progress_max)
        self.progressbar.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
        self.progress.set(0)
        btn = tk.Button(self.root, text='start', command=self.start)
        btn.pack(fill=tk.BOTH, expand=True, padx=15, pady=5)
        self.btn = btn
        self.root.mainloop()
    def start(self):
        self.progress.set(0)
        self.btn.config(state=tk.DISABLED)
        self.thread_queue = Queue.Queue() # used to communicate between main thread (UI) and worker thread
        new_thread = threading.Thread(target=self.run_loop, kwargs={'param1':100, 'param2':20})
        new_thread.start()
        # schedule a time-task to check UI
        # it's in main thread, because it's called by self.root
        self.root.after(100, self.listen_for_result)
    def run_loop(self, param1, param2):
        progress = 0
        for entry in range(self.progress_max):
            time.sleep(0.1)
            progress = progress + 1
            self.thread_queue.put(progress)
    def listen_for_result(self):
        '''
        Check if there is something in the queue.
        Must be invoked by self.root to be sure it's running in main thread
        '''
        try:
            progress = self.thread_queue.get(False)
            self.progress.set(progress)
        except Queue.Empty: # must exist to avoid trace-back
            pass
        finally:
            if self.progress.get() < self.progressbar['maximum']:
                self.root.after(100, self.listen_for_result)
            else:
                self.btn.config(state=tk.NORMAL)
if __name__ == '__main__':
    win = MainWindow()
    win.show()

一個(gè)進(jìn)度條。設(shè)定最大進(jìn)度為100。在子線程里每隔0.1秒更新一格。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python 與GO中操作slice,list的方式實(shí)例代碼

    python 與GO中操作slice,list的方式實(shí)例代碼

    這篇文章主要介紹了python 與GO中操作slice,list的方式實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Python數(shù)據(jù)結(jié)構(gòu)之順序表的實(shí)現(xiàn)代碼示例

    Python數(shù)據(jù)結(jié)構(gòu)之順序表的實(shí)現(xiàn)代碼示例

    這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)之順序表的實(shí)現(xiàn)代碼示例,簡(jiǎn)單介紹了順序表的相關(guān)內(nèi)容,然后分享了其代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • 利用Python實(shí)現(xiàn)文件讀取與輸入以及數(shù)據(jù)存儲(chǔ)與讀取的常用命令

    利用Python實(shí)現(xiàn)文件讀取與輸入以及數(shù)據(jù)存儲(chǔ)與讀取的常用命令

    這篇文章主要給大家介紹了關(guān)于利用Python實(shí)現(xiàn)文件讀取與輸入以及數(shù)據(jù)存儲(chǔ)與讀取的常用命令,文中還介紹了用python循環(huán)保存文件并循環(huán)讀取文件的方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • Tensorflow tf.nn.depthwise_conv2d如何實(shí)現(xiàn)深度卷積的

    Tensorflow tf.nn.depthwise_conv2d如何實(shí)現(xiàn)深度卷積的

    這篇文章主要介紹了Tensorflow tf.nn.depthwise_conv2d如何實(shí)現(xiàn)深度卷積的,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Python如何自定義鄰接表圖類

    Python如何自定義鄰接表圖類

    這篇文章主要介紹了Python如何自定義鄰接表圖類問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • python實(shí)現(xiàn)求純色彩圖像的邊框

    python實(shí)現(xiàn)求純色彩圖像的邊框

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)求純色彩圖像的邊框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • Python?變量類型實(shí)例詳解

    Python?變量類型實(shí)例詳解

    這篇文章主要介紹了Python?變量類型實(shí)例詳解,基于變量的數(shù)據(jù)類型,解釋器會(huì)分配指定內(nèi)存,并決定什么數(shù)據(jù)可以被存儲(chǔ)在內(nèi)存中,接下來(lái)更多詳細(xì)內(nèi)容需要的小伙伴可以參考下面文章,希望對(duì)你有所幫助
    2022-02-02
  • Python讀取excel中的圖片完美解決方法

    Python讀取excel中的圖片完美解決方法

    excel中的圖片非常常見(jiàn),但是通過(guò)python讀取excel中的圖片沒(méi)有很好的解決辦法。今天小編給大家分享一種比較聰明的方法,感興趣的朋友跟隨腳本之家小編看看吧
    2018-07-07
  • vc6編寫(xiě)python擴(kuò)展的方法分享

    vc6編寫(xiě)python擴(kuò)展的方法分享

    有些C/C++的代碼要在Python中要用到,又不想轉(zhuǎn)成python,所以就寫(xiě)成python的擴(kuò)展來(lái)調(diào)用,以下是我嘗試后,在VC6下編寫(xiě)python擴(kuò)展的過(guò)程
    2014-01-01
  • Python實(shí)現(xiàn)嵌套列表的7中方法總結(jié)

    Python實(shí)現(xiàn)嵌套列表的7中方法總結(jié)

    這篇文章主要來(lái)給大家講解一個(gè)Python的進(jìn)階知識(shí)點(diǎn):如何將一個(gè)嵌套的大列表展開(kāi)形成一個(gè)列表。小編提供了7種方法供大家學(xué)習(xí)參考,希望大家能喜歡
    2023-03-03

最新評(píng)論