我试图在代码执行期间显示弹出窗口小部件,并在执行结束时将其销毁。
from PySide import QtGui, QtCore
import sys
import time
def spinner(fn):
def wrapper(*args, **kwargs):
cls = args[0]
splash = QtGui.QWidget (cls)
splash.setWindowFlags(QtCore.Qt.FramelessWindowHint)
rect = cls.rect()
# rect.setTop (rect.height()-50)
splash.setGeometry(rect)
palette = QtGui.QPalette()
palette.setColor(QtGui.QPalette.Background, QtGui.QColor(0,0,0,150))
splash.setAutoFillBackground(True)
splash.setPalette(palette)
splash.setAttribute(QtCore.Qt.WA_TransparentForMouseEvents)
splash.show()
fn( *args, **kwargs)
splash.destroy()
return wrapper
@spinner
def action (w):
time.sleep (10)
def main():
app = QtGui.QApplication ( sys.argv )
w = QtGui.QDialog ()
l = QtGui.QHBoxLayout(w)
b = QtGui.QPushButton ('Hello world')
l.addWidget (b)
b.clicked.connect(lambda :action(w))
w.show()
sys.exit( app.exec_() )
if __name__ == "__main__":
main()
但是只有最后才出现......有一个解决方法吗? 干杯
答案 0 :(得分:2)
Qt有QSplashScreen
类,这使得管理启动画面变得如此简单:
splashPixmap = QPixmap('MyImage.png')
splash = QSplashScreen(splashPixmap, Qt.WindowStaysOnTopHint)
splash.setMask(splashPixmap.mask())
splash.show()
app.processEvents()
time.sleep(2)
...
这显示一个启动画面,其中包含带有透明部分的图像,并且睡眠时间为2秒。