1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
from PyQt4 import QtGui
from PyQt4.QtCore import QEvent, QTimer, Qt
import time
class QApplication(QtGui.QApplication):
timestamp = None
def notify (self, receiver, event):
if event.type() in [ QEvent.MouseMove, QEvent.KeyPress ]:
self.timestamp = time.time()
return super().notify(receiver, event);
class Login(QtGui.QDialog):
def __init__(self, parent=None, username=None):
super(Login, self).__init__(parent)
self.username = QtGui.QLineEdit(self)
if username:
self.username.insert(username)
self.password = QtGui.QLineEdit(self)
self.button = QtGui.QPushButton('Login', self)
self.button.clicked.connect(self.on_login)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.username)
layout.addWidget(self.password)
layout.addWidget(self.button)
def on_login(self):
if (self.username.text() == 'foo' and
self.password.text() == 'bar'):
self.accept()
else:
QtGui.QMessageBox.warning(
self, 'Error', 'Bad user or password')
class Window(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
if __name__ == '__main__':
app = QApplication([])
timeout = 5
def idletick():
global timeout
if time.time() - app.timestamp < 1.5:
timeout = 5
else:
timeout -= 1
if timeout < 0:
idletimer.stop()
login = Login(None, username)
result = login.exec_()
assert result == QtGui.QDialog.Accepted
idletimer.start(1000)
login = Login()
if login.exec_() == QtGui.QDialog.Accepted:
idletimer = QTimer()
idletimer.timeout.connect(idletick)
username = login.username.text()
idletimer.start(1000)
window = Window()
window.show()
app.exec_() |
Partager