这篇文章主要介绍python如何试下PyQt5/Pyside2按钮右击菜单,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
具体代码如下所述:
import sys from PySide2.QtGui import * from PySide2.QtCore import * from PySide2.QtWidgets import * class MainForm(QMainWindow): def __init__(self, parent=None): super(MainForm, self).__init__(parent) # create button self.button = QPushButton("test button", self) self.button.resize(100, 30) # set button context menu policy self.button.setContextMenuPolicy(Qt.CustomContextMenu) self.button.customContextMenuRequested.connect(self.on_context_menu) # create context menu self.popMenu = QMenu(self) self.popMenu.addAction(QAction('test0', self)) self.popMenu.addAction(QAction('test1', self)) self.popMenu.addSeparator() self.popMenu.addAction(QAction('test2', self)) def on_context_menu(self, point): # show context menu self.popMenu.exec_(self.button.mapToGlobal(point)) def main(): app = QApplication(sys.argv) form = MainForm() form.show() app.exec_() if __name__ == '__main__': main()
以上是“python如何试下PyQt5/Pyside2按钮右击菜单”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。