这是使用Pyside的示例代码,用于显示按钮单击时的进度条。问题是按下按钮后,GUI会冻结,直到任务完成(此处为5秒)。有没有办法克服这个问题?
import sys
import time
try:
from PySide import QtCore
from PySide import QtGui
except ImportError:
from PyQt4 import QtCore
from PyQt4 import QtGui
class DemoBtn(QtGui.QWidget):
def __init__(self):
super(DemoBtn, self).__init__()
x, y, w, h = 500, 400, 400, 200
self.setGeometry(x, y, w, h)
x, y, w, h = 30, 10, 96, 32
btn = QtGui.QPushButton("Push button", self)
self.pbar = QtGui.QProgressBar(self)
btn.setGeometry(x, y, w, h)
btn.clicked.connect(self._btn_cb)
self.pbar.setGeometry(30, 50, 200, 25)
def _btn_cb(self):
self.pbar.setMaximum(0)
self.pbar.setMinimum(0)
time.sleep(5) # some time consuming task here
print "clicked"
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
demo = DemoBtn()
demo.show()
sys.exit(app.exec_())
修改1:
似乎问题只出在无限期进度条(忙碌进度条)上。