from PyQt5.Qt import *
import sys
class MyWindow(QWidget):
# 自定义窗体
def contextMenuEvent(self, evt: QContextMenuEvent) -> None:
menu = QMenu(self)
new_action = QAction('打开', menu)
new_action.triggered.connect(self.open)
close_action = QAction('关闭', menu)
close_action.triggered.connect(self.close)
menu.addAction(new_action)
menu.addAction(close_action)
# point 调用
menu.exec_(evt.globalPos())
def open(self):
print('open')
def close(self):
print('close')
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MyWindow()
win.setGeometry(300, 300, 500, 400)
win.show()
sys.exit(app.exec_())
运行结果: