欧美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), 
    # 當(dāng)然也可以將他們分別與不同函數(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()) #在鼠標(biāo)位置顯示

關(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), 
    # 當(dāng)然也可以將他們分別與不同函數(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
    # 菜單顯示前,將它移動到鼠標(biāo)點擊的位置 
    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())))
  # 計算圖標(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按鈕右鍵菜單功能的實現(xiàn)代碼,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!

相關(guān)文章

  • Python xpath表達式如何實現(xiàn)數(shù)據(jù)處理

    Python xpath表達式如何實現(xiàn)數(shù)據(jù)處理

    這篇文章主要介紹了Python xpath表達式如何實現(xiàn)數(shù)據(jù)處理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • 利用Python刪除電腦中重復(fù)文件的方法

    利用Python刪除電腦中重復(fù)文件的方法

    這篇文章主要介紹了利用Python刪除電腦中的重復(fù)文件,下文我們來分享解決電腦中文件重復(fù)的情況的一個方法,需要的朋友可以參考一下,希望對大家日常問題解決有所幫助
    2022-05-05
  • Python辦公自動化批量處理文件實現(xiàn)示例

    Python辦公自動化批量處理文件實現(xiàn)示例

    這篇文章主要為大家介紹了Python辦公自動化批量處理文件實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • python實現(xiàn)鳶尾花三種聚類算法(K-means,AGNES,DBScan)

    python實現(xiàn)鳶尾花三種聚類算法(K-means,AGNES,DBScan)

    這篇文章主要介紹了python實現(xiàn)鳶尾花三種聚類算法(K-means,AGNES,DBScan),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Tensorflow的DataSet的使用詳解

    Tensorflow的DataSet的使用詳解

    本文主要介紹了Tensorflow的DataSet的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • 詳解Python 序列化Serialize 和 反序列化Deserialize

    詳解Python 序列化Serialize 和 反序列化Deserialize

    這篇文章主要介紹了詳解Python 序列化Serialize 和 反序列化Deserialize的相關(guān)資料,序列化是將對象狀態(tài)轉(zhuǎn)換為可保持或傳輸?shù)母袷降倪^程。與序列化相對的是反序列化,它將流轉(zhuǎn)換為對象。這兩個過程結(jié)合起來,可以輕松地存儲和傳輸數(shù)據(jù),需要的朋友可以參考下
    2017-08-08
  • Python+Pillow進行圖形處理的示例詳解

    Python+Pillow進行圖形處理的示例詳解

    PIL:Python Imaging Library,已經(jīng)是Python平臺事實上的圖像處理標(biāo)準(zhǔn)庫了。PIL功能非常強大,但API卻非常簡單易用。本文就將利用Pillow進行簡單的圖形處理,需要的可以參考一下
    2022-10-10
  • python序列化與數(shù)據(jù)持久化實例詳解

    python序列化與數(shù)據(jù)持久化實例詳解

    這篇文章主要介紹了python序列化與數(shù)據(jù)持久化,結(jié)合實例形式詳細(xì)分析了Python序列化與數(shù)據(jù)持久化相關(guān)原理、實現(xiàn)技巧與操作注意事項,需要的朋友可以參考下
    2019-12-12
  • python 字典修改鍵(key)的幾種方法

    python 字典修改鍵(key)的幾種方法

    這篇文章主要介紹了python 字典修改鍵(key)的幾種方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • Python環(huán)境Pillow( PIL )圖像處理工具使用解析

    Python環(huán)境Pillow( PIL )圖像處理工具使用解析

    這篇文章主要介紹了Python環(huán)境Pillow( PIL )圖像處理工具使用解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-09-09

最新評論