Pyside2添加右键菜单
作者:YXN-python 阅读量:11 发布日期:2024-11-05
from PySide2.QtWidgets import QApplication, QFrame, QMenu, QAction, QVBoxLayout, QWidget
class MyFrame(QFrame):
def __init__(self, parent=None):
super().__init__(parent)
def contextMenuEvent(self, event):
# 创建上下文菜单
context_menu = QMenu(self)
# 添加刷新按钮
refresh_action = QAction("刷新", self)
refresh_action.triggered.connect(self.refresh)
context_menu.addAction(refresh_action)
# 在鼠标点击位置显示菜单
context_menu.exec_(event.globalPos())
def refresh(self):
print("刷新中...")
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(100, 100, 600, 400)
self.frame = MyFrame()
layout = QVBoxLayout(self)
layout.addWidget(self.frame)
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
YXN-python
2024-11-05