人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動(dòng)態(tài)

Python鼠標(biāo)事件及坐標(biāo)獲取窗口和屏幕坐標(biāo)

發(fā)布日期:2021-12-27 15:50 | 文章來源:CSDN

本文主要介紹重寫自帶的一些方法,拾取屏幕和窗口坐標(biāo)信息

效果如下:

分析:

定時(shí)服務(wù):在固定一段時(shí)間后執(zhí)行相關(guān)的函數(shù)方法,例如這里表示的是在0秒后執(zhí)行self.giveHelp這個(gè)方法

QTimer.singleShot(0, self.giveHelp)

重寫關(guān)閉事件

 def giveHelp(self):
  self.text = "請(qǐng)點(diǎn)擊這里觸發(fā)追蹤鼠標(biāo)的功能"
  self.update()
# self.update()表示的是刷新

重寫上下文菜單事件

#這里的重寫上下文菜單表示的是右鍵所顯示的菜單
def contextMenuEvent(self, event):
 menu = QMenu(self)
 oneAction = menu.addAction("&one")
 #這里表示的是右鍵后再選擇出現(xiàn)時(shí)連接的信號(hào)和槽函數(shù)
 oneAction.triggered.connect(self.one)
 # 這里表示的是添加一行分割線
 menu.addSeparator()
 menu.exec_(event.globaPos)

重寫自帶的繪制事件 也就是自定義

def paintEvent(self, event):
 text = self.text
 i = text.find("\n\n")
 if i>=0:
  text = text[0:1]
 if self.key:這個(gè)表示的是如果觸發(fā)了鍵盤事件那么這里就記錄按鈕事件的信息
 text += "\n\n你按下了: {0}".format(self.key)
 painter = QPainter(self)
 painter.setRebderHint(QPainter.TextAntialiasing)
 painter.drawText(self.rect(), Qt.AlignCenter, text)#繪制文本信息

重新實(shí)現(xiàn)調(diào)整窗口大小事件

也就是說在界面窗口大小出現(xiàn)變化的時(shí)候回調(diào)用這個(gè)方法

def resizeEvent(self,event):
 self.text = "調(diào)整窗口大小為: QSize({0},{1})".format(event.size().width(), event.size().height())
 self.update()

重新實(shí)現(xiàn)鼠標(biāo)的釋放事件

也就是說當(dāng)鼠標(biāo)雙擊完成或者是單擊完成之后調(diào)用的方法

 def mouseReleaseEvent(self, event):

重新實(shí)現(xiàn)鼠標(biāo)的移動(dòng)事件

也就是說在鼠標(biāo)移動(dòng)的過程中就會(huì)調(diào)用這個(gè)方法

def mouseMoveEvent(self, event):
  # 這里的意思就是將窗口的坐標(biāo)轉(zhuǎn)化為屏幕的坐標(biāo)
  globalPos = self.mapTpGlobal(event.pos())
  self.text = """鼠標(biāo)的位置為: 窗口坐標(biāo)為: Qpoint({0},{1}),屏幕坐標(biāo)為:屏幕坐標(biāo)為:QPoint({2},{3})""".format(
  event.pos().x(),event.pos().y(), globalPos.x(), globalPos.y())
  )
  self.update()

重新實(shí)現(xiàn)鼠標(biāo)雙擊事件

 def mouseDoubleClickEvent(self, event):
  self.justDoubleClicked = True
  self.text = "你雙擊了鼠標(biāo)"
  self.update()

重新實(shí)現(xiàn)鍵盤按下事件

def keyPressEvent(self, event):
 if event.key()==QT.Key_Home:
  self.key = "Home"

源碼如下

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")
  # 這里表示6秒鐘后調(diào)用giveHelp這個(gè)方法
  QTimer.singleShot(6000, self.giveHelp)  # 避免窗口大小重繪事件的影響,可以把參數(shù)0改變成3000(3秒),然后在運(yùn)行,就可以明白這行代碼的意思。
 def giveHelp(self):
  self.text = "請(qǐng)點(diǎn)擊這里觸發(fā)追蹤鼠標(biāo)功能"
  self.update() # 重繪事件,也就是觸發(fā)paintEvent函數(shù)。
 '''重新實(shí)現(xiàn)關(guān)閉事件'''
 def closeEvent(self, event):
  print("Closed")
 '''重新實(shí)現(xiàn)上下文菜單事件,也就是右鍵的菜單'''
 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("Three")
threeAction.triggered.connect(self.three)
  menu.exec_(event.globalPos())
 '''上下文菜單槽函數(shù)'''
 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()
 '''重新實(shí)現(xiàn)繪制事件'''
 def paintEvent(self, event):
  text = self.text
  i = text.find("\n\n")
  if i >= 0:
text = text[0:i]
  if self.key: # 若觸發(fā)了鍵盤按鈕,則在文本信息中記錄這個(gè)按鈕信息。
text += "\n\n你按下了: {0}".format(self.key)
  painter = QPainter(self)
  painter.setRenderHint(QPainter.TextAntialiasing)
  painter.drawText(self.rect(), Qt.AlignCenter, text) # 繪制信息文本的內(nèi)容
  if self.message: # 若消息文本存在則在底部居中繪制消息,5秒鐘后清空消息文本并重繪。
painter.drawText(self.rect(), Qt.AlignBottom | Qt.AlignHCenter,
 self.message)
QTimer.singleShot(5000, self.clearMessage)
QTimer.singleShot(5000, self.update)
 '''清空消息文本的槽函數(shù)'''
 def clearMessage(self):
  self.message = ""
 '''重新實(shí)現(xiàn)調(diào)整窗口大小事件'''
 def resizeEvent(self, event):
  self.text = "調(diào)整窗口大小為: QSize({0}, {1})".format(
event.size().width(), event.size().height())
  self.update()
 '''重新實(shí)現(xiàn)鼠標(biāo)釋放事件'''
 def mouseReleaseEvent(self, event):
  # 若鼠標(biāo)釋放為雙擊釋放,則不跟蹤鼠標(biāo)移動(dòng)
  # 若鼠標(biāo)釋放為單擊釋放,則需要改變跟蹤功能的狀態(tài),如果開啟跟蹤功能的話就跟蹤,不開啟跟蹤功能就不跟蹤
  if self.justDoubleClicked:
self.justDoubleClicked = False
  else:
self.setMouseTracking(not self.hasMouseTracking()) # 單擊鼠標(biāo)
if self.hasMouseTracking():
 self.text = "開啟鼠標(biāo)跟蹤功能.\n" + \
"請(qǐng)移動(dòng)一下鼠標(biāo)!\n" + \
"單擊鼠標(biāo)可以關(guān)閉這個(gè)功能"
else:
 self.text = "關(guān)閉鼠標(biāo)跟蹤功能.\n" + \
"單擊鼠標(biāo)可以開啟這個(gè)功能"
self.update()
 '''重新實(shí)現(xiàn)鼠標(biāo)移動(dòng)事件'''
 def mouseMoveEvent(self, event):
  if not self.justDoubleClicked:
globalPos = self.mapToGlobal(event.pos()) # 窗口坐標(biāo)轉(zhuǎn)換為屏幕坐標(biāo)
self.text = """鼠標(biāo)位置:
窗口坐標(biāo)為:QPoint({0}, {1}) 
屏幕坐標(biāo)為:QPoint({2}, {3}) """.format(event.pos().x(), event.pos().y(), globalPos.x(), globalPos.y())
self.update()
 '''重新實(shí)現(xiàn)鼠標(biāo)雙擊事件'''
 def mouseDoubleClickEvent(self, event):
  self.justDoubleClicked = True
  self.text = "你雙擊了鼠標(biāo)"
  self.update()
 '''重新實(shí)現(xiàn)鍵盤按下事件'''
 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)
 '''重新實(shí)現(xiàn)其他事件,適用于PyQt沒有提供該事件的處理函數(shù)的情況,Tab鍵由于涉及焦點(diǎn)切換,不會(huì)傳遞給keyPressEvent,因此,需要在這里重新定義。'''
 def event(self, event):
  if (event.type() == QEvent.KeyPress and
  event.key() == Qt.Key_Tab):
self.key = "在event()中捕獲Tab鍵"
self.update()
return True
  return QWidget.event(self, event)
if __name__ == "__main__":
 app = QApplication(sys.argv)
 form = Widget()
 form.show()
 app.exec_()

以上就是Python鼠標(biāo)事件及坐標(biāo)獲取窗口和屏幕坐標(biāo)的詳細(xì)內(nèi)容,更多關(guān)于鼠標(biāo)事件及坐標(biāo)獲取窗口和屏幕坐標(biāo)的資料請(qǐng)關(guān)注本站其它相關(guān)文章!

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開通

自選配置、實(shí)時(shí)開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對(duì)1客戶咨詢顧問

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部