python PyQt5Pyside2 按钮右击菜单实例代码

python PyQt5Pyside2 按钮右击菜单实例代码,第1张

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 按钮右击菜单实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/3257575.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-10-04
下一篇 2022-10-04

发表评论

登录后才能评论

评论列表(0条)

保存