【发布时间】: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