gpt4 book ai didi

Python - PyQT4如何检测窗口中任意位置的鼠标点击位置?

转载 作者:太空狗 更新时间:2023-10-29 17:46:45 24 4
gpt4 key购买 nike

我有一个 1024x768 分辨率的窗口,当单击或鼠标悬停时,我想找到 x、y 值。我该怎么做?

import sys
from PyQt4 import QtGui, QtCore


class Example(QtGui.QWidget):

def __init__(self):
super(Example, self).__init__()

self.initUI()

def initUI(self):

qbtn = QtGui.QPushButton('Quit', self)
#qbtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
qbtn.clicked.connect(self.test)
qbtn.resize(qbtn.sizeHint())
qbtn.move(50, 50)

self.setGeometry(0, 0, 1024, 768)
self.setWindowTitle('Quit button')
self.setWindowFlags(self.windowFlags() | QtCore.Qt.FramelessWindowHint)
self.show()

def test(self):
print "show the position of mouse cursor in screen resolution: x is ?? , y is ??"

def main():

app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())


if __name__ == '__main__':
main()

最佳答案

import sys
from PyQt5 import QtWidgets, QtGui, QtCore

class Example(QtWidgets.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def mousePressEvent(self, QMouseEvent):
print(QMouseEvent.pos())
def mouseReleaseEvent(self, QMouseEvent):
cursor = QtGui.QCursor()
print(cursor.pos())
def initUI(self):
qbtn = QtWidgets.QPushButton('Quit', self)
qbtn.resize(qbtn.sizeHint())
qbtn.move(50, 50)
self.setGeometry(0, 0, 1024, 768)
self.setWindowTitle('Quit button')
self.setWindowFlags(self.windowFlags() | QtCore.Qt.FramelessWindowHint)
self.show()
def main():
app = QtWidgets.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

输出:

PyQt4.QtCore.QPoint(242, 285)
PyQt4.QtCore.QPoint(1741, 423)
PyQt4.QtCore.QPoint(439, 372)

关于Python - PyQT4如何检测窗口中任意位置的鼠标点击位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19825650/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com