datetime:2019/5/23 10:53
author:nzb
右键菜单
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu
from PyQt5 import QtGui
class UI_demo(QMainWindow):
"""用户界面"""
def __init__(self):
super().__init__()
self.title = 'PyQt5 Context Menu'
self.left = 600
self.top = 200
self.width = 500
self.height = 400
self.iconName = '../img/home.ico'
self.initWindow()
def initWindow(self):
self.setWindowIcon(QtGui.QIcon(self.iconName))
self.setGeometry(self.left, self.top, self.width, self.height)
self.setWindowTitle(self.title)
self.show()
def contextMenuEvent(self, event):
"""右键菜单"""
contextMenu = QMenu(self)
newAction = contextMenu.addAction("New")
openAction = contextMenu.addAction("Open")
quitAction = contextMenu.addAction("Quit")
action = contextMenu.exec_(self.mapToGlobal(event.pos()))
if action == quitAction:
self.close()
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = UI_demo()
sys.exit(app.exec_())