资源下载 - 从此开始!

下载我_精品源码软件教程资源网

您的位置:主页 > 网络编程 > 正文

python PyQt5/Pyside2 按钮右击菜单实例代码

2019-08-17 22:16 来源:互联网 编辑:admin

具体代码如下所述:

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 按钮右击菜单实例代码,希望对大家有所帮助!

打赏一下,我们会为大家提供更多优质资源!

相关文章,95%的人会看!{有内幕、有真相...}

看过本文的人还看过

微信扫一扫

站长微信账号