基本功能实现了,但是不够漂亮
屏蔽了Alt+F4,但是Ctrl+Alt+Del没能屏蔽
# -*- coding: utf-8 -*-
u"""
一个挂机锁软件。
author: Mpc
last edited: Mat 2014
"""
import sys
import random
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
#Label
lbl1 = QtGui.QLabel(u‘密 码:‘, self)
lbl1.setAlignment(QtCore.Qt.AlignRight)
lbl2 = QtGui.QLabel(u‘确认密码:‘, self)
#LineEdit
self.le1 = QtGui.QLineEdit(self)
self.le2 = QtGui.QLineEdit(self)
#PushButton
okButton = QtGui.QPushButton("OK")
okButton.clicked.connect(self.okButton_reponds)
cancelButton = QtGui.QPushButton("Cancel")
cancelButton.clicked.connect(self.cancelButton_reponds)
#hbox1
hbox1 = QtGui.QHBoxLayout()
hbox1.addStretch(1)
hbox1.addWidget(lbl1)
hbox1.addWidget(self.le1)
hbox1.addStretch(1)
#hbox2
hbox2 = QtGui.QHBoxLayout()
hbox2.addStretch(1)
hbox2.addWidget(lbl2)
hbox2.addWidget(self.le2)
hbox2.addStretch(1)
#hbox3
hbox3 = QtGui.QHBoxLayout()
hbox3.addStretch(1)
hbox3.addWidget(okButton)
hbox3.addWidget(cancelButton)
hbox3.addStretch(1)
vbox = QtGui.QVBoxLayout()
vbox.addStretch(1)
vbox.addLayout(hbox1)
vbox.addLayout(hbox2)
vbox.addLayout(hbox3)
vbox.addStretch(1)
self.setLayout(vbox)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle(u‘挂机锁‘)
self.show()
def okButton_reponds(self):
if not self.le1.text().compare(self.le2.text()):
if self.le1.text() == ‘‘:
QtGui.QMessageBox.information(self, ‘Error‘, u‘请键入密码!!‘)
else:
self.lockScreen = LockScreen()
self.lockScreen.password_lock = self.le1.text()
self.close()
else:
QtGui.QMessageBox.information(self, ‘Error‘, u‘密码不一致‘)
self.le1.setText(‘‘)
self.le2.setText(‘‘)
def keyPressEvent(self, event):
keyEvent = QtGui.QKeyEvent(event)
if keyEvent.key() == QtCore.Qt.Key_Return:
self.okButton_reponds()
def cancelButton_reponds(self):
self.le1.setText(‘‘)
self.le2.setText(‘‘)
class LockScreen(QtGui.QWidget):
def __init__(self):
super(LockScreen, self).__init__()
self.initUI()
#password
self.password_lock = ‘‘
def initUI(self):
#Label
lbl1 = QtGui.QLabel(u‘密 码:‘, self)
lbl1.setAlignment(QtCore.Qt.AlignRight)
lbl2 = QtGui.QLabel(u‘挂机中.......请勿乱动!!‘, self)
lbl2.setAlignment(QtCore.Qt.AlignRight)
lbl2.setFont(QtGui.QFont(u"华文行楷", 50, 20))
#LineEdit
self.le1 = QtGui.QLineEdit(self)
#PushButton
okButton = QtGui.QPushButton("OK")
okButton.clicked.connect(self.okButton_reponds)
cancelButton = QtGui.QPushButton("Cancel")
cancelButton.clicked.connect(self.cancelButton_reponds)
#hbox1
hbox1 = QtGui.QHBoxLayout()
hbox1.addStretch(1)
hbox1.addWidget(lbl1)
hbox1.addWidget(self.le1)
hbox1.addStretch(1)
#hbox2
hbox2 = QtGui.QHBoxLayout()
hbox2.addStretch(1)
hbox2.addWidget(lbl2)
hbox2.addStretch(1)
#hbox3
hbox3 = QtGui.QHBoxLayout()
hbox3.addStretch(1)
hbox3.addWidget(okButton)
hbox3.addWidget(cancelButton)
hbox3.addStretch(1)
vbox = QtGui.QVBoxLayout()
vbox.addStretch(1)
vbox.addLayout(hbox2)
vbox.addStretch(1)
vbox.addLayout(hbox1)
vbox.addLayout(hbox3)
vbox.addStretch(1)
self.setLayout(vbox)
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
self.showFullScreen()
#backgroundimage
self.setAutoFillBackground(True)
palette = QtGui.QPalette()
# palette.setBrush(QtGui.QPalette.Window, QtGui.QBrush(QtGui.QPixmap("g:/Python/pictures/" +str(random.randint(1, 500))+".jpg").scaled(self.size(), QtCore.Qt.IgnoreAspectRatio, QtCore.Qt.SmoothTransformation)))
palette.setBrush(QtGui.QPalette.Window, QtGui.QBrush(QtGui.QColor(170, 58, 146)))
self.setPalette(palette)
self.setWindowTitle(u‘挂机锁‘)
self.show()
def okButton_reponds(self):
if self.password_lock == self.le1.text():
QtCore.QCoreApplication.instance().quit()
elif self.le1.text() == ‘‘:
QtGui.QMessageBox.information(self, ‘Error‘, u‘请键入密码!!‘)
elif (not (self.le1.text() == ‘‘) and not self.password_lock == self.le1.text()):
QtGui.QMessageBox.information(self, ‘Error‘, u‘密码错误!!‘)
self.le1.setText(‘‘)
def keyPressEvent(self, event):
keyEvent = QtGui.QKeyEvent(event)
if keyEvent.key() == QtCore.Qt.Key_Return:
self.okButton_reponds()
#屏蔽Alt+F4
def closeEvent(self, event):
event.ignore()
def cancelButton_reponds(self):
self.le1.setText(‘‘)
self.le2.setText(‘‘)
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == ‘__main__‘:
main()
一个用Python写的简易挂机锁,布布扣,bubuko.com
一个用Python写的简易挂机锁