# -*- coding: utf-8 -*-
# python:2.x
__author__ = 'Administrator'
import sys
from PyQt4.QtCore import Qt
from PyQt4 import QtGui,QtCore,Qt
from rol import Ui_Form
class Example(QtGui.QDialog,Ui_Form):
def __init__(self,parnet=None):
super(Example, self).__init__(parnet)
self.setupUi(self)
self.createContextMenu()
self.label.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.label.customContextMenuRequested.connect(self.conmenx)
self.pushButton.clicked.connect(self.butoonMenu)
def conmenx(self,point):
self.menu.exec_(self.label.mapToGlobal(point))
def createContextMenu(self):
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.menu=QtGui.QMenu(self)
menu1=self.menu.addAction(u'打开')
menu1.triggered.connect(self.opens)
def butoonMenu(self):
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.buttonme=QtGui.QMenu(self)
text=self.buttonme.addAction(u'打开')
text.triggered.connect(self.opens)
self.buttonme.exec_(self.mapToGlobal(QtCore.QPoint(240,230)))
def opens(self):
QtGui.QMessageBox.information(self,'x','xx')
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
main()
图片如下: