Python 名称错误:全局名称';QContextMenuEvent';没有定义

Python 名称错误:全局名称';QContextMenuEvent';没有定义,python,menu,pyqt5,Python,Menu,Pyqt5,我想添加一个右键单击菜单 def contextMenuEvent(self,event): global posX global posY global selections,scan_widget if event.reason() == QContextMenuEvent.Mouse: menu = QMenu(self) clear = menu.addAction('Clear') for i in s

我想添加一个右键单击菜单

def contextMenuEvent(self,event):
    global posX
    global posY
    global selections,scan_widget

    if event.reason() == QContextMenuEvent.Mouse:
        menu = QMenu(self)
        clear = menu.addAction('Clear')

        for i in selections:
            self.buttonLabels.append(menu.addAction(i))

        deleteBox = menu.addAction('Delete Box')
        changeId = menu.addAction('Change Id')
        cancel = menu.addAction('Cancel')
        action = menu.exec_(self.mapToGlobal(event.pos()))
        for i,key in enumerate(self.buttonLabels):
            if action == key:
                self.annotClass = selections[i]
                self.annotEnabled = True
        if action == deleteBox:
            self.deleteEnabled = True
        elif action == changeId:
            #Call the textbox
            self.newBoxId = textBox()
            self.newBoxId.setGeometry(QRect(500, 100, 300, 100))
            self.newBoxId.show()
        elif action == cancel:
            pass
        elif action == clear:
            self.annotClass = 'Clear'
            self.annotEnabled = True

        self.posX_annot = event.pos().x()
        self.posY_annot = event.pos().y()

        posX = event.pos().x()
        posY = event.pos().y()

        self.repaint()
        self.buttonLabels = []
    self.annotEnabled = False
但请告诉我这个错误:

NameError:未定义全局名称“QContextMenuEvent”

然后,我添加了QContextMenuEvent作为导入:

from PyQt5.QtWidgets import (QApplication, QComboBox, QHBoxLayout, QPushButton,QSizePolicy, QVBoxLayout, QWidget,QLineEdit, QInputDialog, QMenu,QContextMenuEvent)
但是,

ImportError:无法导入名称QContextMenuEvent


我做错了什么?

从我搜索的内容来看,
QContextMenuEvent
类驻留在
QtGui
模块中,可以从
PyQt5

from PyQt5.QtGui import QContextMenuEvent

我在谷歌上搜索到,
QContextMenuEvent
类驻留在
QtGui
模块中,可以从
PyQt5

from PyQt5.QtGui import QContextMenuEvent
我换了

if event.reason()==QContextMenuEvent.Mouse:

如果event.button==Qt.RightButton:
我被替换

if event.reason()==QContextMenuEvent.Mouse:


如果event.button==Qt.RightButton:

您能在终端中执行“pip冻结”吗?看起来您缺少程序包当前未安装程序“pip”。您可以通过键入:sudo apt install python来安装它。您是如何安装PyQt5的?这就是该库所需的python模块。您可以在终端中执行“pip冻结”吗?看起来您缺少程序包当前未安装程序“pip”。您可以通过键入:sudo apt install python来安装它。您是如何安装PyQt5的?这就是LibraryImporter所需的python模块错误:没有安装并检查名为qtgui的模块
PyQt5
。模块
PyQt5
具有
QtGui
模块。请再次检查。导入错误:未安装并检查名为QtGuiI的模块。模块
PyQt5
具有
QtGui
模块。请再查一下。