分離UI我缺少的東西在這裏。雖然在兩個獨立的線程中運行,但UI仍未按預期更新。它仍然滯後於dd工作線程。線程在Python不是從工作線程
from ui import Ui_main_window
from PyQt4 import QtGui, QtCore
import sys
import subprocess
import commands
import threading
from time import sleep
out_int = 0
def _dd_thread_run(_if, _of, _bs, _size):
_dd_subprocess_command_format = "dd if=%s bs=%s | pv -n --size %s | dd of=%s" % (_if, _bs, _size, _of)
_dd_subprocess_command = [_dd_subprocess_command_format]
_dd_progress = subprocess.Popen(_dd_subprocess_command, shell=True, stderr=subprocess.PIPE)
while _dd_progress.poll() is None:
out = _dd_progress.stderr.readline().replace("\n", "")
global out_int
out_int = int (out)
def _ui_progress_set():
class MainWindow(QtGui.QMainWindow, Ui_main_window):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)
app = QtGui.QApplication(sys.argv)
ui = MainWindow()
ui.show()
while True:
for i in range(100):
ui.progressBar.setValue(out_int)
sleep(.1)
t1 = threading.Thread(target=_dd_thread_run, args = ["/dev/urandom", "/dev/null", "100K", "100M"])
t1.start()
t2 = threading.Thread(target=_ui_progress_set, args = [])
t2.start()
我懷疑Python或PyQt的bug?而且它保持不變,無論UI類是在哪裏定義的。
的可能的複製(http://stackoverflow.com/questions/2482437/label-in-pyqt4-gui-not-updating-with-every-loop [PyQt4中GUI標籤不與FOR循環的每一個循環更新] -of-for-loop) –
請注意,Qt只能從主線程訪問,這也可能解釋了您在其他一些問題中看到的崩潰。您需要重新構建您的程序,以便在主線程中使用Qt,並使用'QThread'將信號發送回主線程以更新GUI。 –