Python版本:3.10.8
库: PyQt6、pyautogui、sys
有帮助的话,点个赞再走呗,谢谢啦~
完整代码↓
from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtCore import QTimer
import pyautogui
class Ui_mouse_tracer(object):
def setupUi(self, mouse_tracer):
mouse_tracer.setObjectName("mouse_tracer")
mouse_tracer.resize(250, 100)
self.widget = QtWidgets.QWidget(mouse_tracer)
self.widget.setGeometry(QtCore.QRect(16, 10, 181, 81))
self.widget.setObjectName("widget")
self.gridLayout = QtWidgets.QGridLayout(self.widget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.position = QtWidgets.QLabel(self.widget)
self.position.setObjectName("position")
self.gridLayout.addWidget(self.position, 0, 0, 1, 1)
self.screen = QtWidgets.QLabel(self.widget)
self.screen.setObjectName("screen")
self.gridLayout.addWidget(self.screen, 1, 0, 1, 1)
self.retranslateUi(mouse_tracer)
QtCore.QMetaObject.connectSlotsByName(mouse_tracer)
self.Timer()
def retranslateUi(self, mouse_tracer):
_translate = QtCore.QCoreApplication.translate
mouse_tracer.setWindowTitle(_translate("mouse_tracer", "mouse_tracer"))
self.position.setText(_translate("mouse_tracer", ""))
self.screen.setText(_translate("mouse_tracer", str(pyautogui.size())))
def Timer(self):
self.timer = QTimer()
self.timer.timeout.connect(self.tracing) # 设定PyQt6的计时器
self.timer.start(200)
def tracing(self):
a = pyautogui.position() # 用pyautogui获取鼠标位置
self.position.setText(str(a))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
mouse_tracer = QtWidgets.QWidget()
ui = Ui_mouse_tracer()
ui.setupUi(mouse_tracer)
mouse_tracer.show()
sys.exit(app.exec())