pyqt5 鼠标操作

#资料 http://blog.sina.com.cn/s/blog_6483fa330102xo6w.html
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtGui import QCursor
from PyQt5.QtCore import Qt class Demo(QWidget): def __init__(self):
super(Demo, self).__init__()
self.label = QLabel('Hello World', self)
self.label1 = QLabel('喂 世界', self)
self.label1.move(0, 30)
self.label2 = QLabel('鼠标位置', self)
self.resize(500, 300)
self.label.resize(200, 20)
self.label1.resize(200, 20)
self.label2.resize(400, 20)
self.label2.move(0, 60)
self.label3 = QLabel('鼠标位置', self)
self.label3.resize(400, 20)
self.label3.move(0, 90) self.setMouseTracking(True) # 设置鼠标移动跟踪是否有效
'''
设置为True时,只要鼠标在窗口内移动时mouseMoveEvent事件就能捕获
设置为False时(默认),只有鼠标键按下并在窗口内移动时mouseMoveEvent事件才能捕获
注意只能是QWidget,如果是QMainwindow,则无效
self.hasMouseTracking()返回设置的状态
''' def mousePressEvent(self, event): # 鼠标键按下时调用(任意一个键,按一下调一次),这些方法是许多控件自带的,这里来自于QWidget。
self.label.setText('鼠标键按下了')
n = event.button() # 用来判断是哪个鼠标健触发了事件【返回值:0 1 2 4】
'''
QtCore.Qt.NoButton - 0 - 没有按下鼠标键
QtCore.Qt.LeftButton -1 -按下鼠标左键
QtCore.Qt.RightButton -2 -按下鼠标右键
QtCore.Qt.Mion 或 QtCore.Qt.MiddleButton -4 -按下鼠标中键
'''
nn = event.buttons() # 返回前面所列枚举值的组合,用于判断同时按下了哪些键【不知怎么判断】 <PyQt5.QtCore.Qt.MouseButtons object at 0x0000003326982F98> def mouseReleaseEvent(self, event): #鼠标键释放时调用
#参数1:鼠标的作用对象;参数2:鼠标事件对象,用来保存鼠标数据
self.label.setText('鼠标键放开了') def mouseMoveEvent(self, event): # 鼠标移动事件
ret = self.hasMouseTracking() #返回鼠标MouseTracking的状态
self.label1.setText('鼠标移动了:%s' % ret)
x = event.x() # 返回鼠标相对于窗口的x轴坐标
y = event.y() # 返回鼠标相对于窗口的y轴坐标
self.label2.setText('鼠标x坐标:%s ,鼠标y坐标:%s' % (x, y))
xy = event.pos() #返回鼠标坐标 ,QPoint(463, 0) 相对于控件 【用xy.x() xy.y()提取值】
s=event.localPos() #返回鼠标坐标 相对于控件 QPointF(2.0, 2.0)
s = self.mapToGlobal(xy) # 将窗口坐标转换成屏幕坐标.属于QWidget类的方法;参数类型QPoint
self.label3.setText('鼠标x坐标:%s ,鼠标y坐标:%s' % (xy.x(), xy.y()))
xy1 = event.globalPos() # 返回鼠标相对于屏幕的坐标。PyQt5.QtCore.QPoint(1096, 37)【用xy1.x() xy1.y()提取值】
s1 = self.mapFromGlobal(xy1) #将屏幕坐标转换成窗口坐标.属于QWidget类的方法;参数类型QPoint
# mapToParent(QPoint) - 将窗口坐标转换成父窗口坐标。如果没有父窗口,则相当于mapToGlobal (QPoint)
# mapFromParent(QPoint) - 将父窗口坐标转换成窗口坐标。如果没有父窗口,则相当于mapFromGlobal(QPoint)
# mapTo (QWidget, QPoint) - 将窗口坐标转换成 QWidget父窗口坐标
px = event.globalX() # 返回相对于屏幕的x坐标
py = event.globalY() # 返回相对于屏幕的y坐标
s = event.windowPos() # 相对于窗口的坐标(保留一位小数),PyQt5.QtCore.QPointF(481.0, 1.0)【用s.x() s.y()提取值】
p = event.screenPos() # 相对于屏幕的坐标(保留一位小数).PyQt5.QtCore.QPointF(476.0, 49.0)【用p.x() p.y()提取值】
t = event.timestamp() # 返回事件发生的时间。【以程序运行开始计时,以毫秒为单位】 def mouseDoubleClickEvent(self, event): # 鼠标双击时调用
self.label1.setText('双击了鼠标')
'''双击时的事件顺序如下:
MouseButtonPress
MouseButtonRelease
MouseButtonDblClick
MouseButtonPress
MouseButtonRelease
QApplicaption类的setDoubleClickInterval( )方法可设置双击的时间间隔;doubleClickInterval( )方法返回双击的时间间隔。''' def enterEvent(self, event): # 鼠标移进时调用
print('鼠标移进窗口了')
self.setCursor(Qt.CrossCursor) # 设置鼠标形状。
#需要from PyQt5.QtGui import QCursor,from PyQt5.QtCore import Qt
#鼠标形状对照图见下方
# self.unsetCursor() #鼠标恢复系统默认 def leaveEvent(self, event): # 鼠标移出时调用
print('鼠标移出窗口了') def wheelEvent(self, event): # 滚轮滚动时调用。event是一个QWheelEvent对象
angle = event.angleDelta() # 返回滚轮转过的数值,单位为1/8度.PyQt5.QtCore.QPoint(0, 120)
angle = angle / 8 # 除以8之后单位为度。PyQt5.QtCore.QPoint(0, 15) 【向前滚是正数,向后滚是负数 用angle.y()取值】
ang = event.pixelDelta() # 返回滚轮转过的像素值 【不知为何 没有值】
# print(event.x(),event.y()) #返回鼠标相对于窗口的坐标
w = event.pos() # 返回相对于控件的当前鼠标位置.PyQt5.QtCore.QPoint(260, 173)
w1 = event.posF() # 返回相对于控件的当前鼠标位置.PyQt5.QtCore.QPointF(302.0, 108.0)
# 坐标函数与上面相同 if __name__ == '__main__':
app = QApplication(sys.argv)
demo = Demo()
demo.show()
sys.exit(app.exec())
鼠标形状对应图

QCursor 自定义鼠标:
from PyQt5.QtWidgets import QApplication, QWidget,QLabel,QPushButton
import sys
from PyQt5.QtGui import QCursor
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter, QPixmap class win(QWidget): #创建一个类,为了集成控件
def __init__(self):
super(win, self).__init__()
self.setWindowTitle('窗口标题')
self.resize(400,300)
self.setup_ui()#控件布局函数 def setup_ui(self): #控件布局
label = QLabel('标签控件', self)
button = QPushButton('按钮', self)
label.move(10, 10)
label.setStyleSheet('background-color: green')
button.move(300, 10) pm=QPixmap('subiao.png') #创建一张像素图纸
pm1=pm.scaled(20,20) #缩放pm图纸,赋值给图纸pm1
#参数 图纸的宽高
cursor=QCursor(pm1,0,0) #创建鼠标对象
#参数1 图纸
#参数2 参数3 鼠标热点坐标;默认-1,图纸中心点;0,0图纸左上角
label.setCursor(cursor) # 设置鼠标形状 if __name__=='__main__':
app=QApplication(sys.argv) #创建应用
window=win()
window.show()
sys.exit(app.exec_())
unsetCursor() 重置形状-鼠标恢复默认
获取鼠标对象:
cursor() -> QCursor 获取鼠标对象
pm2=self.cursor() #获取鼠标对象
s=pm2.pos() #提取鼠标位置,相对于屏幕 QPoint(1184, 62)
pm2.setPos(0,0) #设置鼠标位置

pyqt5 鼠标操作的更多相关文章
- Python模拟键盘输入和鼠标操作
Python模拟键盘输入和鼠标操作 一.Python键盘输入模拟: import win32api import win32con win32api.keybd_event(17,0,0,0) #c ...
- c# 鼠标操作
1#region 3using System; 4using System.Runtime.InteropServices; 6#endregion 8namespace Windows.Forms. ...
- opencv鼠标操作及GUI矩形绘画
OpenCV的鼠标操作是通过一个中介函数配合回调函数来实现的.指定鼠标操作消息回调函数的函数为SetMouseCallback. void setMouseCallback(const string& ...
- WPF 中模拟键盘和鼠标操作
转载:http://www.cnblogs.com/sixty/archive/2009/08/09/1542210.html 更多经典文章:http://www.qqpjzb.cn/65015.ht ...
- python selenium-webdriver 元素操作之鼠标操作(四)
上节内容主要说明了元素的定位,本节内容说要说对元素的操作,元素的操作分为两部分一部分是鼠标的操作,另一种是对键盘对元素的操作,下面我们主要讲解一下鼠标对元素的操作. webdriver 模块中几种比较 ...
- python-web自动化-鼠标操作
鼠标操作由ActionChains类来完成鼠标操作 perform() 执行鼠标操作move_to_element() 鼠标悬浮:最常用的操作double_click() 双击操作context_cl ...
- Selenium基础知识(二)鼠标操作
一.鼠标操作 这个需要使用webdriver下的ActionChains类,这个类是操作鼠标操作的: from selenium.webdriver import ActionChains 鼠标操作可 ...
- selenium自动化之鼠标操作
在做自动化测试的时候,经常会遇到这种情况,某个页面元素,你必须要把鼠标移动到上面才能显示出元素.那么这种情况,我们怎么处理呢?,selenium给我们提供了一个类来处理这类事件——ActionChai ...
- 【python】鼠标操作
[python]鼠标操作 推荐地址:http://www.cnblogs.com/fnng/p/3288444.html --------------------------------------- ...
随机推荐
- Kivy crash 中文教程 实例入门 1. 第1个应用 Kivy App (Making a simple App)
1. 空白窗口 在 PyCharm 中创建一个名为 TutorialApp 的项目,然后在该项目中新建了个名为 tutorial_app.py 的 Python 源文件,在 PyCharm 的代码编 ...
- linux shell << 注释多行
#!/bin/bash #script name: a.sh #author: aaron <<EOF echo "line 1" echo "line 2& ...
- 初学者学习C++的50条忠告
1.把C++当成一门新的语言学习(和C没啥关系!真的.); 2.看<Thinking In C++>,不要看<C++变成死相>; 3.看<The C++ Programm ...
- Freemarker空值判断
freemarker中显示某对象使用${name}. 但如果name为null,freemarker就会报错.如果需要判断对象是否为空: <#if name??> …… </#if& ...
- 遍历List、Map删除元素
遍历List删除元素 方法一: List<String> list = new ArrayList<>(); list.add("1"); list.add ...
- SharePoint 2013 批量导入、删除帐号
删除一个group里所有的帐号: cls ########################### # "Enter the site URL here" $SITEURL = &q ...
- bower介绍
一. bower是什么? bower是twitter推出的第三方依赖管理工具.其特点是对包结构没有强制规范,也因此bower本身并不提供一套构建工具,它充当的基本上是一个静态资源的共享平台.它可用于搜 ...
- Elasticsearch GC 时间过长的解决方法
前言:GC 时间过长是个常见的问题,下文我将对应的现象和解决方案进行阐述.为什么这么解决,可以参考我的另外一个博客中的内存使用和GC指标这个章节 我们有时会发现elasticsearch集群挂掉,或者 ...
- 【POJ3974】最长回文字串
在这里采用的是哈希+二分的方法. 根据回文串的性质可知,可以将回文分成奇回文和偶回文分别进行处理. 对于奇回文来说,每次枚举的端点一定是重合的,因此只需计算出端点左右公共的长度是多少即可,因此二分的是 ...
- [hihocoder1509][异或排序]
hihocoder1509 思路 对于每两个数,从二进制的高位到低位考虑,发现,若前面一个的当前位是1,后面一个的当前位置是0,那么s的当前位置必须是1.反之,若前面是0,后面是1,那么s的当前位置必 ...