python之PyQt按鈕右鍵菜單功能的實(shí)現(xiàn)代碼
實(shí)現(xiàn)效果如下圖:
這篇文字主要寫了兩方面的內(nèi)容:
第一是按鈕的自定義,第二是右鍵菜單的使用,不僅是按鈕的右鍵菜單,其他一些控件的右鍵菜單也可以類似創(chuàng)建和使用。
關(guān)于右鍵菜單則是QMenu的一些使用方法有:
樣式表的使用:
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;}" )
右鍵菜單的創(chuàng)建和菜單的信號(hào)槽:
def createContextMenu(self): ''''' 創(chuàng)建右鍵菜單 ''' # 必須將ContextMenuPolicy設(shè)置為Qt.CustomContextMenu # 否則無法使用customContextMenuRequested信號(hào) self.setContextMenuPolicy(Qt.CustomContextMenu) self.customContextMenuRequested.connect(self.showContextMenu) # 創(chuàng)建QMenu self.contextMenu = QMenu(self) self.actionA = self.contextMenu.addAction(QIcon("images/0.png"),u'| 動(dòng)作A') self.actionB = self.contextMenu.addAction(QIcon("images/0.png"),u'| 動(dòng)作B') self.actionC = self.contextMenu.addAction(QIcon("images/0.png"),u'| 動(dòng)作C') #添加二級(jí)菜單 self.second = self.contextMenu.addMenu(QIcon("images/0.png"),u"| 二級(jí)菜單") self.actionD = self.second.addAction(QIcon("images/0.png"),u'| 動(dòng)作A') self.actionE = self.second.addAction(QIcon("images/0.png"),u'| 動(dòng)作B') self.actionF = self.second.addAction(QIcon("images/0.png"),u'| 動(dòng)作C') # 將動(dòng)作與處理函數(shù)相關(guān)聯(lián) # 這里為了簡單,將所有action與同一個(gè)處理函數(shù)相關(guān)聯(lián), # 當(dāng)然也可以將他們分別與不同函數(shù)關(guān)聯(lián),實(shí)現(xiàn)不同的功能 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()) #在鼠標(biāo)位置顯示
關(guān)于按鈕的自定義,則包括了一些事件的重新定義和對按鈕的ui界面的重新設(shè)計(jì)和繪制,就不一一列舉了。
下面是一個(gè)demo包括了按鈕的自定義,右鍵菜單的創(chuàng)建和使用,包括兩個(gè)文件,圖片可以隨便找一個(gè),不要過大或者過小就行:
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): ''''' 創(chuàng)建右鍵菜單 ''' # 必須將ContextMenuPolicy設(shè)置為Qt.CustomContextMenu # 否則無法使用customContextMenuRequested信號(hào) self.setContextMenuPolicy(Qt.CustomContextMenu) self.customContextMenuRequested.connect(self.showContextMenu) # 創(chuàng)建QMenu self.contextMenu = QMenu(self) self.actionA = self.contextMenu.addAction(QIcon("images/0.png"),u'| 動(dòng)作A') self.actionB = self.contextMenu.addAction(QIcon("images/0.png"),u'| 動(dòng)作B') self.actionC = self.contextMenu.addAction(QIcon("images/0.png"),u'| 動(dòng)作C') #添加二級(jí)菜單 self.second = self.contextMenu.addMenu(QIcon("images/0.png"),u"| 二級(jí)菜單") self.actionD = self.second.addAction(QIcon("images/0.png"),u'| 動(dòng)作A') self.actionE = self.second.addAction(QIcon("images/0.png"),u'| 動(dòng)作B') self.actionF = self.second.addAction(QIcon("images/0.png"),u'| 動(dòng)作C') # 將動(dòng)作與處理函數(shù)相關(guān)聯(lián) # 這里為了簡單,將所有action與同一個(gè)處理函數(shù)相關(guān)聯(lián), # 當(dāng)然也可以將他們分別與不同函數(shù)關(guān)聯(lián),實(shí)現(xiàn)不同的功能 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): ''''' 右鍵點(diǎn)擊時(shí)調(diào)用的函數(shù) ''' self.count+=1 # 菜單顯示前,將它移動(dòng)到鼠標(biāo)點(diǎn)擊的位置 self.contextMenu.exec_(QCursor.pos()) #在鼠標(biāo)位置顯示 #self.contextMenu.show() print self.count def actionHandler(self): ''''' 菜單中的具體action調(diào)用的函數(shù) ''' 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()) # 重畫圖標(biāo) if not self.icon().isNull(): painter.drawPixmap(iconPos, QPixmap(self.icon().pixmap(self.iconSize()))) # 計(jì)算圖標(biāo)和文本大小位置 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_()
總結(jié)
以上所述是小編給大家介紹的python之PyQt按鈕右鍵菜單功能的實(shí)現(xiàn)代碼,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
- python編程PyQt5創(chuàng)建按鈕及觸發(fā)點(diǎn)擊事件示例解析
- python GUI庫圖形界面開發(fā)之PyQt5切換按鈕控件QPushButton詳細(xì)使用方法與實(shí)例
- python GUI庫圖形界面開發(fā)之PyQt5單選按鈕控件QRadioButton詳細(xì)使用方法與實(shí)例
- python PyQt5/Pyside2 按鈕右擊菜單實(shí)例代碼
- Python中PyQt5/PySide2的按鈕控件使用實(shí)例
- python之pyqt5通過按鈕改變Label的背景顏色方法
- Python PYQT界面點(diǎn)擊按鈕隨機(jī)變色功能
相關(guān)文章
python pyecharts 實(shí)現(xiàn)一個(gè)文件繪制多張圖
這篇文章主要介紹了python pyecharts 實(shí)現(xiàn)一個(gè)文件繪制多張圖,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05零基礎(chǔ)寫python爬蟲之抓取百度貼吧并存儲(chǔ)到本地txt文件改進(jìn)版
前面已經(jīng)發(fā)了一篇關(guān)于百度貼吧抓取的代碼,今天我們來看下代碼的改進(jìn)版,參考了上篇抓取糗事百科的思路,給需要的小伙伴們參考下吧2014-11-11Python實(shí)現(xiàn)將多張圖片合成視頻并加入背景音樂
這篇文章主要為大家介紹了如何利用Python實(shí)現(xiàn)將多張圖片合成mp4視頻,并加入背景音樂。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-04-04純Python開發(fā)的nosql數(shù)據(jù)庫CodernityDB介紹和使用實(shí)例
這篇文章主要介紹了純Python開發(fā)的nosql數(shù)據(jù)庫CodernityDB介紹和使用實(shí)例,本文實(shí)例包含數(shù)據(jù)插入、數(shù)據(jù)更新、數(shù)據(jù)刪除、數(shù)據(jù)查詢等,需要的朋友可以參考下2014-10-10Pytorch實(shí)現(xiàn)將模型的所有參數(shù)的梯度清0
這篇文章主要介紹了Pytorch實(shí)現(xiàn)將模型的所有參數(shù)的梯度清0,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06Python中單引號(hào)、雙引號(hào)和三引號(hào)具體的用法及注意點(diǎn)
這篇文章主要給大家介紹了關(guān)于Python中單引號(hào)、雙引號(hào)和三引號(hào)具體的用法及注意點(diǎn)的相關(guān)資料,Python中單引號(hào)、雙引號(hào)、三引號(hào)中使用常常困惑,想弄明白這三者相同點(diǎn)和不同點(diǎn),需要的朋友可以參考下2023-07-07