文章目录
- 1. 简介
- 2. Demo
1. 简介
书接上回,PyQt6 使用 QScreen 实现了截图,如果用个控件全屏显示截图,嘿嘿,那就是简单的一个锁屏界面了,类似于一个假死界面,也挺有意思。
友情提醒 输入 quit 可以退出程序
2. Demo
使用一个加密的功能,让 quit 不是以铭文出现
dbd73c2b545209688ed794c0d5413d5a 是 quit的md5码
md5 据说可以被破解,那就还可以改成 使用 bcrpypt ,不停变更 bcrpypt 密钥,会更有意思
友情提醒,退出 是输入 quit
退出 是输入 quit
退出 是输入quit
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QKeyEvent, QPainter, QPaintEvent, QColor
from PyQt6.QtWidgets import QApplication, QWidget
import hashlib
# from cout import coutclass FullScreen(QWidget):def __init__(self):"""构造函数无框 顶部显示, 任务栏不显示输入:quitAttrition:self.pixmap : 主屏幕的截图self.bufs : 字符串缓存"""super(FullScreen, self).__init__()# ------------- 窗口标志设置 -------------self.setWindowFlags(Qt.WindowType.WindowStaysOnTopHint| Qt.WindowType.FramelessWindowHint| Qt.WindowType.Tool) # 可以不出现在任务栏screen = QApplication.primaryScreen()self.pixmap = screen.grabWindow()self.bufs = [] # 用来保存 输入的字符def paintEvent(self, a0: QPaintEvent | None) -> None:painter = QPainter(self)painter.drawPixmap(a0.rect(), self.pixmap)painter.fillRect(a0.rect(), QColor(0, 0, 255, 127))font = painter.font()font.setFamily("楷体")font.setPixelSize(20)painter.setFont(font)painter.setPen(QColor(255, 0, 0))msg = '输入 quit 退出'painter.drawText(20, 30, msg)rect_ad = a0.rect().adjusted(10, 10, -10, -10)painter.drawRect(rect_ad)painter.drawText(rect_ad,Qt.AlignmentFlag.AlignCenter | Qt.TextFlag.TextWrapAnywhere,''.join(self.bufs))return super().paintEvent(a0)# =============== 按键事件 ===============def keyPressEvent(self, a0: QKeyEvent | None) -> None:keycode = a0.key()if keycode <= 0x10ffff:# char = chr(keycode)# cout((keycode, char))self.bufs.append(a0.text())match a0.key():case Qt.Key.Key_Escape:self.bufs = self.bufs[:-1] # 去掉最后一个 esc 字符self.bufs.append('请输入密码 🚶')case Qt.Key.Key_Enter | Qt.Key.Key_Return:bufs_str = ''.join(self.bufs).strip().encode(encoding='utf-8')md5 = hashlib.md5(bufs_str).hexdigest()if md5.casefold() == 'dbd73c2b545209688ed794c0d5413d5a'.casefold():# if bufs_str.strip().casefold() == 'quit'.casefold():app.quit()self.bufs.clear()self.update()if __name__ == '__main__':app = QApplication([])screen = FullScreen()screen.showFullScreen()# cout(screen.rect())app.exec()