欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python之PyQt按鈕右鍵菜單功能的實現(xiàn)代碼

 更新時間:2019年08月17日 15:06:00   作者:一起交流  
這篇文章主要介紹了python PyQt按鈕右鍵菜單功能的實現(xiàn)代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下

實現(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)建和菜單的信號槽:

def createContextMenu(self): 
    ''''' 
    創(chuàng)建右鍵菜單 
    ''' 
    # 必須將ContextMenuPolicy設(shè)置為Qt.CustomContextMenu 
    # 否則無法使用customContextMenuRequested信號 
    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'| 動作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')
    # 將動作與處理函數(shù)相關(guān)聯(lián) 
    # 這里為了簡單,將所有action與同一個處理函數(shù)相關(guān)聯(lián), 
    # 當然也可以將他們分別與不同函數(shù)關(guān)聯(lián),實現(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()) #在鼠標位置顯示

關(guān)于按鈕的自定義,則包括了一些事件的重新定義和對按鈕的ui界面的重新設(shè)計和繪制,就不一一列舉了。
下面是一個demo包括了按鈕的自定義,右鍵菜單的創(chuàng)建和使用,包括兩個文件,圖片可以隨便找一個,不要過大或者過小就行:

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信號 
    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'| 動作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')
    # 將動作與處理函數(shù)相關(guān)聯(lián) 
    # 這里為了簡單,將所有action與同一個處理函數(shù)相關(guān)聯(lián), 
    # 當然也可以將他們分別與不同函數(shù)關(guān)聯(lián),實現(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ào)用的函數(shù) 
    ''' 
    self.count+=1
    # 菜單顯示前,將它移動到鼠標點擊的位置 
    self.contextMenu.exec_(QCursor.pos()) #在鼠標位置顯示
    #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())
      # 重畫圖標
    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_()

總結(jié)

以上所述是小編給大家介紹的python之PyQt按鈕右鍵菜單功能的實現(xiàn)代碼,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!

相關(guān)文章

  • python pyecharts 實現(xiàn)一個文件繪制多張圖

    python pyecharts 實現(xiàn)一個文件繪制多張圖

    這篇文章主要介紹了python pyecharts 實現(xiàn)一個文件繪制多張圖,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • 使用python實現(xiàn)kNN分類算法

    使用python實現(xiàn)kNN分類算法

    這篇文章主要為大家詳細介紹了使用python實現(xiàn)kNN分類算法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • 零基礎(chǔ)寫python爬蟲之抓取百度貼吧并存儲到本地txt文件改進版

    零基礎(chǔ)寫python爬蟲之抓取百度貼吧并存儲到本地txt文件改進版

    前面已經(jīng)發(fā)了一篇關(guān)于百度貼吧抓取的代碼,今天我們來看下代碼的改進版,參考了上篇抓取糗事百科的思路,給需要的小伙伴們參考下吧
    2014-11-11
  • 代碼詳解Python的函數(shù)基礎(chǔ)(2)

    代碼詳解Python的函數(shù)基礎(chǔ)(2)

    這篇文章主要為大家詳細介紹了Python的函數(shù)基礎(chǔ),使用了函數(shù)參數(shù)和遞歸函數(shù),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • python中NumPy的安裝與基本操作

    python中NumPy的安裝與基本操作

    Python雖然也提供了array模塊,但其只支持一維數(shù)組,不支持多維數(shù)組,也沒有各種運算函數(shù),因而不適合數(shù)值運算,NumPy的出現(xiàn)彌補了這些不足,這篇文章主要給大家介紹了關(guān)于python中NumPy的安裝與基本操作的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • Python實現(xiàn)將多張圖片合成視頻并加入背景音樂

    Python實現(xiàn)將多張圖片合成視頻并加入背景音樂

    這篇文章主要為大家介紹了如何利用Python實現(xiàn)將多張圖片合成mp4視頻,并加入背景音樂。文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2022-04-04
  • 純Python開發(fā)的nosql數(shù)據(jù)庫CodernityDB介紹和使用實例

    純Python開發(fā)的nosql數(shù)據(jù)庫CodernityDB介紹和使用實例

    這篇文章主要介紹了純Python開發(fā)的nosql數(shù)據(jù)庫CodernityDB介紹和使用實例,本文實例包含數(shù)據(jù)插入、數(shù)據(jù)更新、數(shù)據(jù)刪除、數(shù)據(jù)查詢等,需要的朋友可以參考下
    2014-10-10
  • python爬取氣象臺每日天氣圖代碼

    python爬取氣象臺每日天氣圖代碼

    大家好,本篇文章主要講的是python爬取氣象臺每日天氣圖代碼,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • Pytorch實現(xiàn)將模型的所有參數(shù)的梯度清0

    Pytorch實現(xiàn)將模型的所有參數(shù)的梯度清0

    這篇文章主要介紹了Pytorch實現(xiàn)將模型的所有參數(shù)的梯度清0,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Python中單引號、雙引號和三引號具體的用法及注意點

    Python中單引號、雙引號和三引號具體的用法及注意點

    這篇文章主要給大家介紹了關(guān)于Python中單引號、雙引號和三引號具體的用法及注意點的相關(guān)資料,Python中單引號、雙引號、三引號中使用常常困惑,想弄明白這三者相同點和不同點,需要的朋友可以參考下
    2023-07-07

最新評論