Python---开发程序右键全屏和退出全屏
最近在学习Python,我想要开发一个软件,记录一下:
简单功能1:实现右键【全屏】和【退出】
from PyQt5.QtWidgets import QApplication, QWidget, QMenu, QAction from PyQt5.QtCore import Qt class MyWindow(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle("我的第一个 PyQt 程序") self.setGeometry(100, 100, 400, 300) # 设置窗口支持右键菜单 self.setContextMenuPolicy(Qt.CustomContextMenu) self.customContextMenuRequested.connect(self.showContextMenu) # 创建右键菜单 self.contextMenu = QMenu(self) # 创建全屏动作 self.fullscreenAction = QAction("全屏", self) self.fullscreenAction.triggered.connect(self.toggleFullscreen) # 创建退出动作 self.exitAction = QAction("退出", self) self.exitAction.triggered.connect(self.close) # 将动作添加到菜单 self.contextMenu.addAction(self.fullscreenAction) self.contextMenu.addAction(self.exitAction) def showContextMenu(self, position): # 根据当前状态更新菜单文本 if self.isFullScreen(): self.fullscreenAction.setText("退出全屏") else: self.fullscreenAction.setText("全屏") # 显示右键菜单 self.contextMenu.exec_(self.mapToGlobal(position)) def toggleFullscreen(self): if self.isFullScreen(): self.showNormal() # 退出全屏 self.fullscreenAction.setText("全屏") else: self.showFullScreen() # 进入全屏 self.fullscreenAction.setText("退出全屏") def keyPressEvent(self, event): # 添加键盘快捷键支持:F11切换全屏,Esc退出全屏 if event.key() == Qt.Key_F11: self.toggleFullscreen() elif event.key() == Qt.Key_Escape and self.isFullScreen(): self.showNormal() self.fullscreenAction.setText("全屏") else: super().keyPressEvent(event) # 创建应用实例 app = QApplication([]) # 创建主窗口 window = MyWindow() # 显示窗口 window.show() # 运行应用 app.exec_()
打完收工!

浙公网安备 33010602011771号