您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關怎么在python中利用PyQt實現一個按鈕右鍵菜單功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
Python是一種跨平臺的、具有解釋性、編譯性、互動性和面向對象的腳本語言,其最初的設計是用于編寫自動化腳本,隨著版本的不斷更新和新功能的添加,常用于用于開發獨立的項目和大型項目。
樣式表的使用:
self.setStyleSheet("QMenu{background:purple;}" "QMenu{border:1px solid lightgray;}" "QMenu{border-color:green;}" "QMenu::item{padding:0px 40px 0px 20px;}" "QMenu::item{height:30px;}" "QMenu::item{color:blue;}" "QMenu::item{background:white;}" "QMenu::item{margin:1px 0px 0px 0px;}" "QMenu::item:selected:enabled{background:lightgray;}" "QMenu::item:selected:enabled{color:white;}" "QMenu::item:selected:!enabled{background:transparent;}" "QMenu::separator{height:50px;}" "QMenu::separator{width:1px;}" "QMenu::separator{background:white;}" "QMenu::separator{margin:1px 1px 1px 1px;}" "QMenu#menu{background:white;}" "QMenu#menu{border:1px solid lightgray;}" "QMenu#menu::item{padding:0px 40px 0px 30px;}" "QMenu#menu::item{height:25px;}" "QMenu#menu::item:selected:enabled{background:lightgray;}" "QMenu#menu::item:selected:enabled{color:white;}" "QMenu#menu::item:selected:!enabled{background:transparent;}" "QMenu#menu::separator{height:1px;}" "QMenu#menu::separator{background:lightgray;}" "QMenu#menu::separator{margin:2px 0px 2px 0px;}" "QMenu#menu::indicator {padding:10px;}" )
右鍵菜單的創建和菜單的信號槽:
def createContextMenu(self): ''''' 創建右鍵菜單 ''' # 必須將ContextMenuPolicy設置為Qt.CustomContextMenu # 否則無法使用customContextMenuRequested信號 self.setContextMenuPolicy(Qt.CustomContextMenu) self.customContextMenuRequested.connect(self.showContextMenu) # 創建QMenu self.contextMenu = QMenu(self) self.actionA = self.contextMenu.addAction(QIcon("images/0.png"),u'| 動作A') self.actionB = self.contextMenu.addAction(QIcon("images/0.png"),u'| 動作B') self.actionC = self.contextMenu.addAction(QIcon("images/0.png"),u'| 動作C') #添加二級菜單 self.second = self.contextMenu.addMenu(QIcon("images/0.png"),u"| 二級菜單") self.actionD = self.second.addAction(QIcon("images/0.png"),u'| 動作A') self.actionE = self.second.addAction(QIcon("images/0.png"),u'| 動作B') self.actionF = self.second.addAction(QIcon("images/0.png"),u'| 動作C') # 將動作與處理函數相關聯 # 這里為了簡單,將所有action與同一個處理函數相關聯, # 當然也可以將他們分別與不同函數關聯,實現不同的功能 self.actionA.triggered.connect(self.actionHandler) self.actionB.triggered.connect(self.actionHandler) self.actionC.triggered.connect(self.actionHandler) self.actionD.triggered.connect(self.actionHandler) self.actionE.triggered.connect(self.actionHandler) self.actionF.triggered.connect(self.actionHandler)
菜單的顯示位置:
self.contextMenu.exec_(QCursor.pos()) #在鼠標位置顯示
關于按鈕的自定義,則包括了一些事件的重新定義和對按鈕的ui界面的重新設計和繪制,就不一一列舉了。
下面是一個demo包括了按鈕的自定義,右鍵菜單的創建和使用,包括兩個文件,圖片可以隨便找一個,不要過大或者過小就行:
mybutton.py # -*- coding: utf-8 -*- from PyQt4.QtCore import Qt, QRect from PyQt4.QtGui import QPushButton, QPainter, QPainterPath, QPen, QColor, QPixmap, QIcon, QBrush, QCursor,QMenu class MenuButton(QPushButton): def __init__(self,parent = None): super(MenuButton,self).__init__(parent) self.setStyleSheet("QMenu{background:purple;}" "QMenu{border:1px solid lightgray;}" "QMenu{border-color:green;}" "QMenu::item{padding:0px 40px 0px 20px;}" "QMenu::item{height:30px;}" "QMenu::item{color:blue;}" "QMenu::item{background:white;}" "QMenu::item{margin:1px 0px 0px 0px;}" "QMenu::item:selected:enabled{background:lightgray;}" "QMenu::item:selected:enabled{color:white;}" "QMenu::item:selected:!enabled{background:transparent;}" "QMenu::separator{height:50px;}" "QMenu::separator{width:1px;}" "QMenu::separator{background:white;}" "QMenu::separator{margin:1px 1px 1px 1px;}" "QMenu#menu{background:white;}" "QMenu#menu{border:1px solid lightgray;}" "QMenu#menu::item{padding:0px 40px 0px 30px;}" "QMenu#menu::item{height:25px;}" "QMenu#menu::item:selected:enabled{background:lightgray;}" "QMenu#menu::item:selected:enabled{color:white;}" "QMenu#menu::item:selected:!enabled{background:transparent;}" "QMenu#menu::separator{height:1px;}" "QMenu#menu::separator{background:lightgray;}" "QMenu#menu::separator{margin:2px 0px 2px 0px;}" "QMenu#menu::indicator {padding:10px;}" ) self.hovered = False self.pressed = False self.pressedIcon = QIcon() self.color = QColor(Qt.gray) self.opacity = 1.0 self.count = 0 # self.setAutoFillBackground(True) # self.setStyleSheet("#Check {background-color: rgb(255, 255, 255);}"); self.createContextMenu() self.count = 0 def createContextMenu(self): ''''' 創建右鍵菜單 ''' # 必須將ContextMenuPolicy設置為Qt.CustomContextMenu # 否則無法使用customContextMenuRequested信號 self.setContextMenuPolicy(Qt.CustomContextMenu) self.customContextMenuRequested.connect(self.showContextMenu) # 創建QMenu self.contextMenu = QMenu(self) self.actionA = self.contextMenu.addAction(QIcon("images/0.png"),u'| 動作A') self.actionB = self.contextMenu.addAction(QIcon("images/0.png"),u'| 動作B') self.actionC = self.contextMenu.addAction(QIcon("images/0.png"),u'| 動作C') #添加二級菜單 self.second = self.contextMenu.addMenu(QIcon("images/0.png"),u"| 二級菜單") self.actionD = self.second.addAction(QIcon("images/0.png"),u'| 動作A') self.actionE = self.second.addAction(QIcon("images/0.png"),u'| 動作B') self.actionF = self.second.addAction(QIcon("images/0.png"),u'| 動作C') # 將動作與處理函數相關聯 # 這里為了簡單,將所有action與同一個處理函數相關聯, # 當然也可以將他們分別與不同函數關聯,實現不同的功能 self.actionA.triggered.connect(self.actionHandler) self.actionB.triggered.connect(self.actionHandler) self.actionC.triggered.connect(self.actionHandler) self.actionD.triggered.connect(self.actionHandler) self.actionE.triggered.connect(self.actionHandler) self.actionF.triggered.connect(self.actionHandler) def showContextMenu(self, pos): ''''' 右鍵點擊時調用的函數 ''' self.count+=1 # 菜單顯示前,將它移動到鼠標點擊的位置 self.contextMenu.exec_(QCursor.pos()) #在鼠標位置顯示 #self.contextMenu.show() print self.count def actionHandler(self): ''''' 菜單中的具體action調用的函數 ''' if self.count%3==1: self.setText(u"first") elif self.count%3==2: self.setText(u"second") elif self.count%3==0: self.setText(u"third") def setEnterCursorType(self, Type): self.cursorType = Type def setColor(self,color): self.color = color def setOpacitys(self,opacity): self.opacity = opacity # self.setOpacity(0.5) def enterEvent(self,event): self.hovered = True self.repaint() QPushButton.enterEvent(self,event) def leaveEvent(self,event): self.hovered = False self.repaint() self.setCursor(QCursor(Qt.ArrowCursor)) QPushButton.leaveEvent(self,event) def mousePressEvent(self, event): self.pressed = True self.repaint() QPushButton.mousePressEvent(self,event) def mouseReleaseEvent(self, event): self.pressed = False self.repaint() QPushButton.mouseReleaseEvent(self,event) def paintEvent(self,event): painter = QPainter(self) btnRect = self.geometry() iconRect = self.iconSize() color = QColor(Qt.black) if self.hovered: color = self.color if self.pressed: color = self.color.darker(120) painter.setPen(QPen(QColor(Qt.lightGray),2)) outline = QPainterPath() outline.addRoundedRect(0, 0, btnRect.width(), btnRect.height(), 0, 0) painter.setOpacity(1) painter.drawPath(outline) painter.setBrush(QBrush(color)) painter.setOpacity(self.opacity) painter_path = QPainterPath() painter_path.addRoundedRect(1, 1, btnRect.width() - 2, btnRect.height() - 2, 0, 0) if self.hovered: painter.setClipPath(painter_path) painter.drawRoundedRect(1, 1, btnRect.width() - 2, btnRect.height() - 2, 0, 0) painter.setOpacity(1) iconPos,textPos = self.calIconTextPos(btnRect, iconRect) # 重畫文本 if not self.text().isNull(): painter.setFont(self.font()) painter.setPen(QPen(QColor(Qt.black),2)) painter.drawText(textPos.x(), textPos.y(), textPos.width(), textPos.height(), Qt.AlignCenter, self.text()) # 重畫圖標 if not self.icon().isNull(): painter.drawPixmap(iconPos, QPixmap(self.icon().pixmap(self.iconSize()))) # 計算圖標和文本大小位置 def calIconTextPos(self,btnSize,iconSize): if self.text().isNull(): iconWidth = iconSize.width()*3/5 iconHeight = iconSize.height()*3/5 else: iconWidth = iconSize.width() iconHeight = iconSize.height() - 50 iconX = (btnSize.width()-iconWidth)/2 iconY = (btnSize.height()-iconHeight)/2 iconPos = QRect() iconPos.setX(iconX) iconPos.setY(iconY) iconPos.setWidth(iconWidth) iconPos.setHeight(iconHeight) textPos = QRect() if not self.text().isNull(): textPos.setX(iconX) textPos.setY(btnSize.height()- 50) textPos.setWidth(iconWidth) textPos.setHeight(50) return (iconPos,textPos) 1 buttontest.py # -*- coding: utf-8 -*- from mybutton import MenuButton import sys from PyQt4.QtCore import QTextCodec, QSize, SIGNAL from PyQt4.QtGui import QDialog, QIcon, QHBoxLayout, QApplication QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8")) class TestDialog(QDialog): def __init__(self,parent=None): super(TestDialog,self).__init__(parent) self.setFixedSize(200,200) self.firMybutton = MenuButton() self.firMybutton.setFixedSize(QSize(100,100)) self.firMybutton.setIcon(QIcon("windows.png")) self.firMybutton.setIconSize(QSize(100,100)) #self.firMybutton.setText(self.tr("確薩")) self.connect(self.firMybutton, SIGNAL("clicked()"),self.cancel) myLayout = QHBoxLayout() myLayout.addWidget(self.firMybutton) self.setLayout(myLayout) def cancel(self): self.close() app=QApplication(sys.argv) dialog=TestDialog() dialog.show() app.exec_()
看完上述內容,你們對怎么在python中利用PyQt實現一個按鈕右鍵菜單功能有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。