gpt4 book ai didi

python - 如何在 PyQt5 中在屏幕上移动一个点?

转载 作者:太空宇宙 更新时间:2023-11-04 05:15:43 35 4
gpt4 key购买 nike

我正在尝试使用 PyQt5 在屏幕上移动一个点。我希望重点始终在移动。我不确定在哪里放置代码来改变它的速度。是否有一些代码不断执行的方法?

这是我的代码:

import sys
from PyQt5.QtWidgets import (QApplication, QLabel, QWidget)
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtCore import Qt

class MouseTracker(QWidget):
distance_from_target = 0
mouse_x_pos = 0
mouse_y_pos = 0
target_x_pos = 500
target_y_pos = 250

def __init__(self):
super().__init__()
self.initUI()
self.setMouseTracking(True)

def initUI(self):
self.setGeometry(200, 200, 1000, 500)
self.setWindowTitle('Mouse Tracker')
self.label = QLabel(self)
self.label.resize(500, 40)
self.show()

def mouseMoveEvent(self, event):
distance_from_target = round(((event.y() - self.target_y_pos)**2 + (event.x() - self.target_x_pos)**2)**0.5)
self.label.setText('Coordinates: (%d : %d)' % (event.x(), event.y()) + " Distance from target: " + str(distance_from_target))
self.mouse_x_pos = event.x()
self.mouse_y_pos = event.y()
self.update()

def mousePressEvent(self, event):
self.target_x_pos = event.x()
self.target_y_pos = event.y()
self.update()

def paintEvent(self, event):
q = QPainter()
q.begin(self)
q.drawLine(self.mouse_x_pos, self.mouse_y_pos, self.target_x_pos, self.target_y_pos)

app = QApplication(sys.argv)
ex = MouseTracker()
sys.exit(app.exec_())

我想做的是让target_x_pos不断增加,不管鼠标是移动还是点击。我该怎么做?

我无法实现循环,因为那样的话就没有其他响应了。这是线程的用武之地吗?

最佳答案

你只需要实现一个QTimer,每次它被触发你必须更新想要的位置,除了使用self.update()来更新绘图.

import sys

from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import (QApplication, QLabel, QWidget)
from PyQt5.QtGui import QPainter


class MouseTracker(QWidget):
distance_from_target = 0
mouse_x_pos = 0
mouse_y_pos = 0
target_x_pos = 500
target_y_pos = 250
vel = 60 # pixels per second

def __init__(self, parent=None):
super(MouseTracker, self).__init__(parent=parent)
self.initUI()
self.setMouseTracking(True)
self.timer = QTimer(self)
self.timer.timeout.connect(self.changePosition)
self.timer.start(1000 / self.vel)

def changePosition(self):
self.target_x_pos += 1
self.update()

def initUI(self):
self.setGeometry(200, 200, 1000, 500)
self.setWindowTitle('Mouse Tracker')
self.label = QLabel(self)
self.label.resize(500, 40)
self.show()

def mouseMoveEvent(self, event):
distance_from_target = round(
((event.y() - self.target_y_pos) ** 2 + (event.x() - self.target_x_pos) ** 2) ** 0.5)
self.label.setText(
'Coordinates: (%d : %d)' % (event.x(), event.y()) + " Distance from target: " + str(distance_from_target))
self.mouse_x_pos = event.x()
self.mouse_y_pos = event.y()
self.update()

def mousePressEvent(self, event):
self.target_x_pos = event.x()
self.target_y_pos = event.y()
self.update()

def paintEvent(self, event):
q = QPainter()
q.begin(self)
q.drawLine(self.mouse_x_pos, self.mouse_y_pos, self.target_x_pos, self.target_y_pos)


app = QApplication(sys.argv)
w = MouseTracker()
sys.exit(app.exec_())

如果你想用 Thread 测试我给你看下面的代码,我在刷新值时没有遇到问题。

import sys
import time
from PyQt5.QtCore import QThread
from PyQt5.QtCore import QTimer
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import (QApplication, QLabel, QWidget)
from PyQt5.QtGui import QPainter


class MyThread(QThread):
signal = pyqtSignal(int)

def __init__(self, parent=None):
super(MyThread, self).__init__(parent=parent)
self.value = None
self.vel = None

def setValue(self, value):
self.value = value

def setVelocity(self, vel):
self.vel = vel

def run(self):
while True:
self.value += 1
self.signal.emit(self.value)
time.sleep(1/self.vel)


class MouseTracker(QWidget):
distance_from_target = 0
mouse_x_pos = 0
mouse_y_pos = 0
target_x_pos = 500
target_y_pos = 250
vel = 60 # pixels per second

def __init__(self, parent=None):
super(MouseTracker, self).__init__(parent=parent)
self.initUI()
self.setMouseTracking(True)
self.thread = MyThread(self)
self.thread.setValue(self.target_x_pos)
self.thread.setVelocity(self.vel)
self.thread.signal.connect(self.updatePosition)
self.thread.start()

def updatePosition(self, val):
self.target_x_pos = val
self.update()

def initUI(self):
self.setGeometry(200, 200, 1000, 500)
self.setWindowTitle('Mouse Tracker')
self.label = QLabel(self)
self.label.resize(500, 40)
self.show()

def mouseMoveEvent(self, event):
distance_from_target = round(
((event.y() - self.target_y_pos) ** 2 + (event.x() - self.target_x_pos) ** 2) ** 0.5)
self.label.setText(
'Coordinates: (%d : %d)' % (event.x(), event.y()) + " Distance from target: " + str(distance_from_target))
self.mouse_x_pos = event.x()
self.mouse_y_pos = event.y()
self.update()

def mousePressEvent(self, event):
self.target_x_pos = event.x()
self.thread.setValue(self.target_x_pos)
self.target_y_pos = event.y()
self.update()

def paintEvent(self, event):
q = QPainter()
q.begin(self)
q.drawLine(self.mouse_x_pos, self.mouse_y_pos, self.target_x_pos, self.target_y_pos)


app = QApplication(sys.argv)
w = MouseTracker()
sys.exit(app.exec_())

关于python - 如何在 PyQt5 中在屏幕上移动一个点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41732808/

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