PySide / wait or sleep

蓝咒 提交于 2019-12-08 01:20:46

问题


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_())
...


来源:https://stackoverflow.com/questions/18317723/pyside-wait-or-sleep

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!