PySide/等待或睡眠 [英] PySide / wait or sleep
问题描述
我是 python 和 pyside 的新手.我将尝试成功运行以下代码.但现在我希望程序在显示窗口后等待用户无法使用它的定义时间,然后升级状态栏.我会尝试 sleep() 但不知道它必须以正确的方式放置在代码中的哪个位置.感谢您的帮助.
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()
推荐答案
不要使用 sleep
.Sleep 只会阻塞活动的事件循环,用户仍然可以点击 GUI 中的任意位置,并且事件将在 sleep
返回后延迟传递.
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/等待或睡眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!