python PyQt5/Pyside2 按钮右击菜单实例代码
具体代码如下所述:
importsys fromPySide2.QtGuiimport* fromPySide2.QtCoreimport* fromPySide2.QtWidgetsimport* classMainForm(QMainWindow): def__init__(self,parent=None): super(MainForm,self).__init__(parent) #createbutton self.button=QPushButton("testbutton",self) self.button.resize(100,30) #setbuttoncontextmenupolicy self.button.setContextMenuPolicy(Qt.CustomContextMenu) self.button.customContextMenuRequested.connect(self.on_context_menu) #createcontextmenu 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)) defon_context_menu(self,point): #showcontextmenu self.popMenu.exec_(self.button.mapToGlobal(point)) defmain(): app=QApplication(sys.argv) form=MainForm() form.show() app.exec_() if__name__=='__main__': main()
总结
以上所所述是小编给大家介绍的pythonPyQt5/Pyside2按钮右击菜单实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!