运行命令时如何在wxpython中制作脉冲进度条?

时间:2019-07-15 16:14:38

标签: python-3.x wxpython progress-bar progressdialog wxpython-phoenix

我想指示我的程序正在运行,并且没有脉冲进度条冻结。在后台中运行的命令是这样的:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import wx
import threading
import common as g


class myThread (threading.Thread):
    def __init__(self, threadID, name):
       threading.Thread.__init__(self)
       self.threadID = threadID
       self.name = name
    def run(self):
       print ("Starting " + self.name)
       my_thread()
       print ("Exiting " + self.name)

class myThread2 (threading.Thread):
    def __init__(self, threadID, name):
       threading.Thread.__init__(self)
       self.threadID = threadID
       self.name = name
    def run(self):
       print ("Starting " + self.name)
       my_thread2()
       print ("Exiting " + self.name)

def my_thread():
     os.system('apt update')

def my_thread2():
    dlg = wx.ProgressDialog('Test', 'Please wait..', style=wx.PD_APP_MODAL | wx.PD_ELAPSED_TIME | wx.PD_CAN_ABORT | wx.STAY_ON_TOP)
    test = OtherFrame(title='progres')
    counter = 1
    while g.th.isAlive():
        print('HEEYY')
        wx.MilliSleep(300)
        dlg.Pulse("Doing computation %d"%counter)
        test.Show()
        counter += 1
class OtherFrame(wx.Frame):

    def __init__(self, title, parent=None):
        wx.Frame.__init__(self, parent=parent, title=title, size=(700, 400))
        self.Centre()
        self.InitUI()
        self.Show()

    def InitUI(self):

        gs = wx.GridSizer(1, 1, 7, 7)
        update = wx.Button(self,label = 'Check for  system Updates')

        gs.Add(update,28,wx.EXPAND)

        update.Bind(wx.EVT_BUTTON, self.OnUpdate)

        self.SetSizer(gs)

    def OnUpdate(self, e):

        g.th = myThread(1, "Thread-1")
        thread2 = myThread2(2, "Thread-2")
        thread2.start()
        g.th.start()
        g.th.join()
        thread2.join()

def main():

    app = wx.App()
    f1 = OtherFrame(title='frame')
    f1.Show()
    app.MainLoop()


if __name__ == '__main__':
    main()

按下按钮,它将开始,我想在弹出对话框中显示对话框过程。

这是相关部分的代码:

@objc dynamic private func audioRouteChangeListener(notification:NSNotification) {
            let audioRouteChangeReason = notification.userInfo![AVAudioSessionRouteChangeReasonKey] as! UInt
            switch audioRouteChangeReason {

            case AVAudioSession.RouteChangeReason.newDeviceAvailable.rawValue:
                print("headphone plugged in")
                DispatchQueue.global().async { [weak self] in

                 **self?.conductor?.loadSamples(byIndex: 2)**

                }

            case AVAudioSession.RouteChangeReason.oldDeviceUnavailable.rawValue:
                print("headphone pulled out")
                DispatchQueue.global().async { [weak self] in

                 **self?.conductor?.loadSamples(byIndex: 2)**
}
            default:
                break
            }
        }

文字“ HEEYY”会在正确的时间显示在正确的位置,但不会显示对话框。

1 个答案:

答案 0 :(得分:0)

您似乎正在尝试使用一个线程来确定另一个线程是否仍在运行。
有一种更简单的方法,请参见下文:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import wx
import threading
#import common as g

class myThread (threading.Thread):
    def __init__(self, threadID, name):
       threading.Thread.__init__(self)
       self.threadID = threadID
       self.name = name
    def run(self):
       print ("Starting " + self.name)
       os.system('apt update')
       print ("Exiting " + self.name)

class OtherFrame(wx.Frame):

    def __init__(self, title, parent=None):
        wx.Frame.__init__(self, parent=parent, title=title, size=(700, 400))
        self.Centre()
        self.InitUI()
        self.Show()

    def InitUI(self):
        gs = wx.GridSizer(1, 1, 7, 7)
        update = wx.Button(self,label = 'Check for  system Updates')
        gs.Add(update,28,wx.EXPAND)
        update.Bind(wx.EVT_BUTTON, self.OnUpdate)
        self.SetSizer(gs)

    def OnUpdate(self, e):
        t1 = myThread(1, "Thread-1")
        t1.start()
        counter = 0
        dlg = wx.ProgressDialog('Test', 'Please wait..', style=wx.PD_APP_MODAL | wx.PD_ELAPSED_TIME | wx.PD_CAN_ABORT | wx.STAY_ON_TOP)
        while t1.isAlive():
            print('HEEYY')
            wx.MilliSleep(300)
            dlg.Pulse("Doing computation %d"%counter)
            wx.GetApp().Yield()
            counter += 1
        del dlg
        t1.join()

def main():
    app = wx.App()
    f1 = OtherFrame(title='frame')
    f1.Show()
    app.MainLoop()

if __name__ == '__main__':
    main()

enter image description here