【问题标题】:How to make pulsing progress bar in wxpython when running command?运行命令时如何在wxpython中制作脉冲进度条?
【发布时间】:2019-07-15 16:14:38
【问题描述】:

我想指示我的程序正在运行并且没有通过脉冲进度条冻结。将在 background 中运行的 command 是这样的:

os.system('apt install program etc...')

它会在按钮按下时开始,我想在此期间显示一个弹出窗口进度对话框过程。

这是相关部分的代码:

#!/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()

文本“HEEYY”在正确的时间出现在正确的位置,但对话框没有出现。

【问题讨论】:

  • 没有多少代码可以使用!
  • 其实没有更多。当此命令在后台运行时,我希望看到一个脉动的进度条。带有线程的东西。
  • 假设存在一个包含按钮的结构和一个用于处理所述按钮的回调函数。那是哪里?
  • 整个代码在这里发布很长,但我设法让两个线程并行运行。现在的问题在这部分代码中(编辑了问题)
  • 您是否尝试在调用dlg.Pulse 之后添加wx.GetApp().Yield()

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


【解决方案1】:

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

#!/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()

【讨论】:

  • 感谢您的回答! keep_going 变量不是必需的,因为 g.th 是另一个线程的名称。如果它结束了,这也将结束。但是随着keep_going,它会永远存在。修改也没有帮助,按下按钮时窗口仍然没有出现在屏幕上。我只能在终端中看到“HEEYY”的东西很好用。
  • 这是一个示例,因为您发布的代码仅够我们猜测! stackoverflow.com/help/minimal-reproducible-example
  • 编辑了问题以包含(我认为)所有必要的代码。
  • 使用完整的工作代码再次编辑(只需要在同一目录中适当的 common.py ),并将代码添加到 my_thread2 以显示测试对话框,它与终端中的文本一起正确显示.
  • 请原谅我的无知,common.py 是什么/在哪里
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多