PySide / wait or sleep

2019-05-22 15:30发布

问题:

i´m new to python and pyside. I´ll try to run the following code with success. But now i want the programm to wait after showing up the window for a defined time where the user can´t use it and then upgrade the statusbar. i´ll tried sleep() but have no idea where it had to be placed the correct way in the code. Thanks for help.

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
ZetCode PySide tutorial 

This program creates a statusbar.

author: Jan Bodnar
website: zetcode.com 
last edited: August 2011
"""

import sys, time
from PySide import QtGui

class Main(QtGui.QMainWindow):

    def __init__(self):
        super(Main, self).__init__()

        self.initUI()

    def initUI(self):               

    exitAction = QtGui.QAction(QtGui.QIcon('exit.png'), '&Exit', self)
    exitAction.setShortcut('Ctrl+Q')
    exitAction.setStatusTip('Exit application')
    exitAction.triggered.connect(self.close)        

    self.statusBar().showMessage('no connection')

    menubar = self.menuBar()
    fileMenu = menubar.addMenu('&File')
    fileMenu.addAction(exitAction)

    self.setGeometry(100, 100, 400,300)
        self.setWindowTitle('Main')    
        self.show() 

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Main()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

回答1:

Don't use sleep. Sleep will only block the active event loop, the user can still click anywhere in the GUI and the events will be delivered delayed after sleep has returned.

If you want to disable user interaction, then disable the widget (and use a timer to reenable it). A simple example in your case could look like this:

...
def main():
    app = QtGui.QApplication(sys.argv)
    ex = Main()
    ex.setEnabled(False)
    QtCore.QTimer.singleShot(4000, lambda: es.setEnabled(True))
    sys.exit(app.exec_())
...


标签: pyside