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

WxPython界面如何用pubsub展示進(jìn)程工作的進(jìn)度條

 更新時(shí)間:2022年11月01日 11:15:31   作者:陳年椰子  
這篇文章主要介紹了WxPython界面如何用pubsub展示進(jìn)程工作的進(jìn)度條,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

用WxPython做界面時(shí), 如果數(shù)據(jù)操作時(shí)間比較長(zhǎng),會(huì)使 WxPython 界面處于假死狀態(tài),用戶體驗(yàn)非常不好。

WxPython是利用pubsub來完成消息的傳送。

下面提供一個(gè)   WxPython界面利用pubsub 展示進(jìn)程工作的進(jìn)度條的例子,實(shí)際使用, 只要修改 

WorkThread 里的 run 內(nèi)容 及 MainFrame 里的 updateDisplay 內(nèi)容即可。

環(huán)境需求

Python 3.7.3
wxPython          4.0.6
Pypubsub          4.0.3

安裝 pubsub

pip install pypubsub
# encoding: utf-8
"""
@author: 陳年椰子
@contact: hndm@qq.com
@version: 1.0
@file: wxpub.py
@time: 2020/02/25  
說明  WxPython 界面利用pubsub與線程通訊使用進(jìn)度條的例子
import wxpub as wp
wp.test()
"""
import wx
from pubsub import pub
from time import sleep
import threading
import sys
 
 
 
# 線程調(diào)用耗時(shí)長(zhǎng)代碼
class WorkThread(threading.Thread):
    def __init__(self):
        """Init Worker Thread Class."""
        threading.Thread.__init__(self)
        self.breakflag = False
        self.start()
 
    def stop(self):
        self.breakflag = True
 
    # 耗時(shí)長(zhǎng)的代碼
    def workproc(self):
        sum_x = 0
        for i in range(1, 101):
            if self.breakflag:
                pub.sendMessage("update", mstatus='中斷')
                sleep(2)
                break
            sum_x = sum_x + i
            sleep(0.1)
            pub.sendMessage("update", mstatus='計(jì)算{} , 合計(jì) {}'.format(i, sum_x))
        return sum_x
 
    def run(self):
        """Run Worker Thread."""
        pub.sendMessage("update", mstatus='workstart')
        result = self.workproc()
        sleep(2)
        pub.sendMessage("update", mstatus='計(jì)算完成,結(jié)果 {}'.format(result))
        pub.sendMessage("update", mstatus='workdone')
 
 
 
class MainFrame(wx.Frame):
    """
    簡(jiǎn)單的界面
    """
 
    def __init__(self, *args, **kw):
        # ensure the parent's __init__ is called
        super(MainFrame, self).__init__(*args, **kw)
 
        # create a panel in the frame
        pnl = wx.Panel(self)
 
        # and put some text with a larger bold font on it
        self.st = wx.StaticText(pnl, label="分析工具 V 2019", pos=(25,25))
        font = self.st.GetFont()
        font.PointSize += 5
        font = font.Bold()
 
        self.st.SetFont(font)
 
 
        # create a menu bar
        self.makeMenuBar()
 
        self.gauge = wx.Gauge(self, range=100, size=(300, 20))
        self.gauge.SetBezelFace(3)
        self.gauge.SetShadowWidth(3)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.st, 0, wx.BOTTOM | wx.ALIGN_CENTER_VERTICAL, 0)
        sizer.Add(self.gauge, 0, wx.BOTTOM | wx.ALIGN_CENTER_VERTICAL, 0)
 
        self.SetSizer(sizer)
 
        # and a status bar
        self.CreateStatusBar()
        self.SetStatusText("啟動(dòng)完成!")
 
        pub.subscribe(self.updateDisplay, "update")
 
 
 
 
    def makeMenuBar(self):
        """
        A menu bar is composed of menus, which are composed of menu items.
        This method builds a set of menus and binds handlers to be called
        when the menu item is selected.
        """
 
        # Make a file menu with Hello and Exit items
        fileMenu = wx.Menu()
        # The "\t..." syntax defines an accelerator key that also triggers
        # the same event
        # helloItem = fileMenu.Append(-1, "&Hello...\tCtrl-H",
        #         "Help string shown in status bar for this menu item")
        self.startItem = fileMenu.Append(-1, "開始",
                "開始計(jì)算")
        self.stopItem = fileMenu.Append(-1, "停止",
                                         "中斷計(jì)算")
        fileMenu.AppendSeparator()
        self.exitItem = fileMenu.Append(-1, "退出",
                "退出")
 
        # Now a help menu for the about item
        helpMenu = wx.Menu()
        aboutItem = helpMenu.Append(-1, "關(guān)于",
                "WxPython 界面與線程通訊的例子")
 
 
        # Make the menu bar and add the two menus to it. The '&' defines
        # that the next letter is the "mnemonic" for the menu item. On the
        # platforms that support it those letters are underlined and can be
        # triggered from the keyboard.
        self.menuBar = wx.MenuBar()
        self.menuBar.Append(fileMenu, "工作")
        self.menuBar.Append(helpMenu, "信息")
 
        # Give the menu bar to the frame
        self.SetMenuBar(self.menuBar)
        self.stopItem.Enable(False)
 
        self.count = 0
 
 
 
        # Finally, associate a handler function with the EVT_MENU event for
        # each of the menu items. That means that when that menu item is
        # activated then the associated handler functin will be called.
        self.Bind(wx.EVT_MENU, self.OnStart, self.startItem)
        self.Bind(wx.EVT_MENU, self.OnStop, self.stopItem)
        self.Bind(wx.EVT_MENU, self.OnExit,  self.exitItem)
        self.Bind(wx.EVT_MENU, self.OnAbout, aboutItem)
 
 
    def OnExit(self, event):
        """Close the frame, terminating the application."""
        try:
            self.work.stop()
        except:
            pass
        self.Close(True)
        sys.exit()
 
 
    def OnStart(self, event):
        self.work = WorkThread()
 
    def OnStop(self, event):
        self.work.stop()
 
 
    def OnAbout(self, event):
        """Display an About Dialog"""
        wx.MessageBox("分析工具 v2019",
                      "關(guān)于",
                      wx.OK|wx.ICON_INFORMATION)
 
    def updateDisplay(self, mstatus):
        """
        Receives data from thread and updates the display
        """
        if mstatus.find("workstart") >= 0:
            self.SetStatusText('開始計(jì)算,代碼不提供中斷線程語句,請(qǐng)等待計(jì)算結(jié)束!')
            self.startItem.Enable(False)
            self.stopItem.Enable(True)
            self.exitItem.Enable(False)
        if mstatus.find("workdone") >= 0:
            self.SetStatusText('完成!')
            self.stopItem.Enable(False)
            self.startItem.Enable(True)
            self.exitItem.Enable(True)
        else:
            self.st.SetLabel(mstatus)
            if mstatus.find(",")>0 and mstatus.find("計(jì)算")>=0:
                mdata = mstatus.split(',')
                # 示范 , 實(shí)際使用需要傳送進(jìn)度
                # print(int(mdata[0].replace('計(jì)算','')))
                g_count = int(mdata[0].replace('計(jì)算',''))
                self.gauge.SetValue(g_count)
 
 
 
def test():
    app = wx.App()
    frm = MainFrame(None, title='分析工具')
    frm.Show()
    app.MainLoop()
 
if __name__=="__main__":
    test()

運(yùn)行后, 點(diǎn)擊 工作-開始

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

相關(guān)文章

  • python爬蟲之PySpider框架的使用

    python爬蟲之PySpider框架的使用

    本文主要介紹了python爬蟲之PySpider框架的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • 如何優(yōu)雅地處理Django中的favicon.ico圖標(biāo)詳解

    如何優(yōu)雅地處理Django中的favicon.ico圖標(biāo)詳解

    默認(rèn)情況下,瀏覽器訪問一個(gè)網(wǎng)站的時(shí)候,同時(shí)還會(huì)向服務(wù)器請(qǐng)求"/favicon.ico"這個(gè)URL,目的是獲取網(wǎng)站的圖標(biāo),下面這篇文章主要給大家介紹了關(guān)于如何優(yōu)雅地處理Django中favicon.ico圖標(biāo)的相關(guān)資料,需要的朋友可以參考下
    2018-07-07
  • 使用Matplotlib創(chuàng)建漂亮的數(shù)據(jù)可視化圖表

    使用Matplotlib創(chuàng)建漂亮的數(shù)據(jù)可視化圖表

    在 Python 中,Matplotlib 是一個(gè)強(qiáng)大而靈活的工具,可以用來創(chuàng)建各種類型的數(shù)據(jù)可視化圖表,本文給大家介紹了如何使用Matplotlib創(chuàng)建漂亮的數(shù)據(jù)可視化圖表,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下
    2024-04-04
  • pytorch中tensor張量數(shù)據(jù)類型的轉(zhuǎn)化方式

    pytorch中tensor張量數(shù)據(jù)類型的轉(zhuǎn)化方式

    今天小編就為大家分享一篇pytorch中tensor張量數(shù)據(jù)類型的轉(zhuǎn)化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • python框架flask入門之路由及簡(jiǎn)單實(shí)現(xiàn)方法

    python框架flask入門之路由及簡(jiǎn)單實(shí)現(xiàn)方法

    這篇文章主要介紹了python框架flask入門路由及路由簡(jiǎn)單實(shí)現(xiàn)方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • 在python里協(xié)程使用同步鎖Lock的實(shí)例

    在python里協(xié)程使用同步鎖Lock的實(shí)例

    今天小編就為大家分享一篇在python里協(xié)程使用同步鎖Lock的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • 基于CentOS搭建Python Django環(huán)境過程解析

    基于CentOS搭建Python Django環(huán)境過程解析

    這篇文章主要介紹了基于CentOS搭建Python Django環(huán)境過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • python有證書的加密解密實(shí)現(xiàn)方法

    python有證書的加密解密實(shí)現(xiàn)方法

    這篇文章主要介紹了python有證書的加密解密實(shí)現(xiàn)方法,采用了M2Crypto組件進(jìn)行相關(guān)的加密解密操作,包含了詳細(xì)的完整實(shí)現(xiàn)過程,需要的朋友可以參考下
    2014-11-11
  • Python開發(fā)入門之如何制作一個(gè)簡(jiǎn)單的桌面應(yīng)用

    Python開發(fā)入門之如何制作一個(gè)簡(jiǎn)單的桌面應(yīng)用

    這篇文章主要給大家介紹了關(guān)于Python開發(fā)入門之如何制作一個(gè)簡(jiǎn)單的桌面應(yīng)用的相關(guān)資料,我們不僅可以使用Python的圖像處理庫,如PIL等來實(shí)現(xiàn)圖片的處理和識(shí)別,同時(shí)你還可以設(shè)計(jì)和開發(fā)具有圖形界面的桌面應(yīng)用程序,需要的朋友可以參考下
    2023-08-08
  • Python嵌套函數(shù)與nonlocal使用詳細(xì)介紹

    Python嵌套函數(shù)與nonlocal使用詳細(xì)介紹

    這篇文章主要介紹了Python嵌套函數(shù)與nonlocal使用,nonlocal關(guān)鍵字與global關(guān)鍵字有點(diǎn)相似,可以對(duì)比著理解。nonlocal關(guān)鍵字只能作用域局部變量,且始終找離當(dāng)前最近的上層局部作用域中的變量
    2022-09-09

最新評(píng)論