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

python3+PyQt5重新實(shí)現(xiàn)QT事件處理程序

 更新時間:2018年04月19日 16:12:46   作者:basisworker  
這篇文章主要為大家詳細(xì)介紹了python3+PyQt5重新實(shí)現(xiàn)QT事件處理程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文是對《Python Qt GUI快速編程》的第10章的例子events用Python3+PyQt5進(jìn)行改寫,涉及到重新實(shí)現(xiàn)QWidget的事件處理程序。本例子涉及到上下文菜單,鼠標(biāo)事件,鍵盤事件,可作為重新實(shí)現(xiàn)事件處理程序的參考。

注:在創(chuàng)建上下文菜單最簡單的方式使用Qwidget.addAction()把動作添加到窗口部件中,再把窗口部件的上下文菜單策略設(shè)置為Qt.ActionsContextMenu即可,但是如果像本例子一樣要根據(jù)不同的狀態(tài)來提供不同的選項(xiàng),則要重新實(shí)現(xiàn)上下文菜單事件處理程序。

#!/usr/bin/env python3
import sys
from PyQt5.QtCore import (QEvent, QTimer, Qt)
from PyQt5.QtWidgets import (QApplication, QMenu, QWidget)
from PyQt5.QtGui import QPainter

class Widget(QWidget):

  def __init__(self, parent=None):
    super(Widget, self).__init__(parent)
    self.justDoubleClicked = False
    self.key = ""
    self.text = ""
    self.message = ""
    self.resize(400, 300)
    self.move(100, 100)
    self.setWindowTitle("Events")
    QTimer.singleShot(0, self.giveHelp) # Avoids first resize msg


  def giveHelp(self):
    self.text = "Click to toggle mouse tracking"
    self.update()


  def closeEvent(self, event):
    print("Closed")


  def contextMenuEvent(self, event):
    menu = QMenu(self)
    oneAction = menu.addAction("&One")
    twoAction = menu.addAction("&Two")
    oneAction.triggered.connect(self.one)
    twoAction.triggered.connect(self.two)
    if not self.message:
      menu.addSeparator()
      threeAction = menu.addAction("Thre&e")
      threeAction.triggered.connect(self.three)
    menu.exec_(event.globalPos())


  def one(self):
    self.message = "Menu option One"
    self.update()


  def two(self):
    self.message = "Menu option Two"
    self.update()


  def three(self):
    self.message = "Menu option Three"
    self.update()


  def paintEvent(self, event):
    text = self.text
    i = text.find("\n\n")
    if i >= 0:
      text = text[0:i]
    if self.key:
      text += "\n\nYou pressed: {0}".format(self.key)
    painter = QPainter(self)
    painter.setRenderHint(QPainter.TextAntialiasing)
    painter.drawText(self.rect(), Qt.AlignCenter, text)
    if self.message:
      painter.drawText(self.rect(), Qt.AlignBottom|Qt.AlignHCenter,
               self.message)
      QTimer.singleShot(5000, self.clearMessage)
      QTimer.singleShot(5000, self.update)

  def clearMessage(self):
    self.message=""

  def resizeEvent(self, event):
    self.text = "Resized to QSize({0}, {1})".format(
              event.size().width(), event.size().height())
    self.update()


  def mouseReleaseEvent(self, event):
    if self.justDoubleClicked:
      self.justDoubleClicked = False
    else:
      self.setMouseTracking(not self.hasMouseTracking())
      if self.hasMouseTracking():
        self.text = "Mouse tracking is on.\n"+\
            "Try moving the mouse!\n"+\
            "Single click to switch it off"
      else:
        self.text = "Mouse tracking is off.\n"+\
                      "Single click to switch it on"
      self.update()


  def mouseMoveEvent(self, event):
    if not self.justDoubleClicked:
      globalPos = self.mapToGlobal(event.pos())
      self.text = "The mouse is at\nQPoint({0}, {1}) "+\
          "in widget coords, and\n"+\
          "QPoint({2}, {3}) in screen coords".format(
          event.pos().x(), event.pos().y(), globalPos.x(),
          globalPos.y())
      self.update()


  def mouseDoubleClickEvent(self, event):
    self.justDoubleClicked = True
    self.text = "Double-clicked."
    self.update()


  def keyPressEvent(self, event):
    self.key = ""
    if event.key() == Qt.Key_Home:
      self.key = "Home"
    elif event.key() == Qt.Key_End:
      self.key = "End"
    elif event.key() == Qt.Key_PageUp:
      if event.modifiers() & Qt.ControlModifier:
        self.key = "Ctrl+PageUp"
      else:
        self.key = "PageUp"
    elif event.key() == Qt.Key_PageDown:
      if event.modifiers() & Qt.ControlModifier:
        self.key = "Ctrl+PageDown"
      else:
        self.key = "PageDown"
    elif Qt.Key_A <= event.key() <= Qt.Key_Z:
      if event.modifiers() & Qt.ShiftModifier:
        self.key = "Shift+"
      self.key += event.text()
    if self.key:
      self.key = self.key
      self.update()
    else:
      QWidget.keyPressEvent(self, event)


  def event(self, event):
    if (event.type() == QEvent.KeyPress and
      event.key() == Qt.Key_Tab):
      self.key = "Tab captured in event()"
      self.update()
      return True
    return QWidget.event(self, event)

if __name__ == "__main__":
  app = QApplication(sys.argv)
  form = Widget()
  form.show()
  app.exec_()

運(yùn)行結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python中使用?zipfile創(chuàng)建文件壓縮工具

    Python中使用?zipfile創(chuàng)建文件壓縮工具

    這篇文章主要介紹了Python中使用zipfile創(chuàng)建文件壓縮工具,通過使用 wxPython 模塊,我們創(chuàng)建了一個簡單而實(shí)用的文件壓縮工具,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的ca參考借鑒價(jià)值,需要的朋友可以參考下
    2023-09-09
  • Python操作MySQL數(shù)據(jù)庫的兩種方式實(shí)例分析【pymysql和pandas】

    Python操作MySQL數(shù)據(jù)庫的兩種方式實(shí)例分析【pymysql和pandas】

    這篇文章主要介紹了Python操作MySQL數(shù)據(jù)庫的兩種方式,結(jié)合實(shí)例形式分析了Python使用pymysql和pandas模塊進(jìn)行mysql數(shù)據(jù)庫的連接、增刪改查等操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-03-03
  • Python+matplotlib實(shí)現(xiàn)計(jì)算兩個信號的交叉譜密度實(shí)例

    Python+matplotlib實(shí)現(xiàn)計(jì)算兩個信號的交叉譜密度實(shí)例

    這篇文章主要介紹了Python+matplotlib實(shí)現(xiàn)計(jì)算兩個信號的交叉譜密度實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • 在Django中輸出matplotlib生成的圖片方法

    在Django中輸出matplotlib生成的圖片方法

    今天小編就為大家分享一篇在Django中輸出matplotlib生成的圖片方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • Flask中app.route裝飾器參數(shù)的使用

    Flask中app.route裝飾器參數(shù)的使用

    app.route()是Flask框架中用于定義路由的裝飾器函數(shù),本文主要介紹了Flask中app.route裝飾器參數(shù)的使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-11-11
  • 如何在Python對Excel進(jìn)行讀取

    如何在Python對Excel進(jìn)行讀取

    這篇文章主要介紹了如何在Python對Excel進(jìn)行讀取,文中講解非常詳細(xì),示例代碼幫助大家參考學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • python中elasticsearch_dsl模塊的使用方法

    python中elasticsearch_dsl模塊的使用方法

    這篇文章主要介紹了python中elasticsearch_dsl模塊的使用方法,elasticsearch-dsl是基于elasticsearch-py封裝實(shí)現(xiàn)的,提供了更簡便的操作elasticsearch的方法
    2022-09-09
  • 對python中矩陣相加函數(shù)sum()的使用詳解

    對python中矩陣相加函數(shù)sum()的使用詳解

    今天小編就為大家分享一篇對python中矩陣相加函數(shù)sum()的使用詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • numpy.where() 用法詳解

    numpy.where() 用法詳解

    這篇文章主要介紹了numpy.where() 用法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • python之文件讀取一行一行的方法

    python之文件讀取一行一行的方法

    今天小編就為大家分享一篇python之文件讀取一行一行的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07

最新評論