I am trying to use PyQt6 to design a program that draws a red circle at the position where the user clicks. However, when I click on the image label, the position is incorrect. Additionally, I set up a list of the positions of the circles, but they are also not in the right place.
from PyQt6.QtWidgets import QApplication, QMainWindow, QLabel
from PyQt6.QtGui import QPixmap, QPainter, QColor
from PyQt6.QtCore import Qt, QPoint
from Ui_dog import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.result = ''
self.bind()
self.setup_image()
self.seted_circle = 2
self.circles_list = [(125, 410), (347, 284), (152, 674), (467, 637), (218, 233), (316, 249)]
# Initialize variables to store coordinates
self.click_x = None
self.click_y = None
def bind(self):
self.Btn_Quit.clicked.connect(self.close)
def setup_image(self):
# Load the image and set it to the QLabel
self.pixmap = QPixmap("Puppies.jpg")
self.dog_image.setPixmap(self.pixmap)
self.dog_image.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.dog_image.setMouseTracking(True)
def mousePressEvent(self, event):
if event.button() == Qt.MouseButton.LeftButton:
if self.dog_image.underMouse():
# Calculate click position relative to dog_image
label_pos = event.position().toPoint() - self.dog_image.pos()
self.click_x = label_pos.x()
self.click_y = label_pos.y()
print(f"Mouse clicked at: x={self.click_x}, y={self.click_y}")
self.update_image() # Update the image with the circle
def update_image(self):
if self.click_x is not None and self.click_y is not None:
# Create a new pixmap to draw on
pixmap = self.pixmap.copy()
painter = QPainter(pixmap)
# Drawing click position for debugging
painter.setPen(QColor(255, 0, 0, 255)) # Red color
painter.setBrush(QColor(255, 0, 0, 255)) # Red color
painter.drawEllipse(QPoint(self.click_x, self.click_y), 10, 10) # Draw red circle at click position
for i in range(len(self.circles_list)):
x, y = self.circles_list[i]
if i < self.seted_circle:
painter.setPen(QColor(0, 255, 0, 100)) # Green color using RGB values
painter.setBrush(QColor(0, 255, 0, 100)) # Green color using RGB values
elif i == self.seted_circle:
painter.setPen(QColor(255, 255, 0, 100)) # Yellow color using RGB values
painter.setBrush(QColor(255, 255, 0, 100)) # Yellow color using RGB values
else:
painter.setPen(QColor(0, 0, 255, 100)) # Blue color using RGB values
painter.setBrush(QColor(0, 0, 255, 100)) # Blue color using RGB values
painter.drawEllipse(QPoint(x, y), 10, 10) # Draw circle
painter.end()
# Update the QLabel with the modified pixmap
self.dog_image.setPixmap(pixmap)
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec()
enter image description here
I tried to change the x and y values, but after making those adjustments, the blue, yellow, and green circles disappeared
for i in range(len(self.circles_list)):
x, y = self.circles_list[i]
if i < self.seted_circle:
painter.setPen(QColor(0, 255, 0, 100)) # Green color using RGB values
painter.setBrush(QColor(0, 255, 0, 100)) # Green color using RGB values
elif i == self.seted_circle:
painter.setPen(QColor(255, 255, 0, 100)) # Yellow color using RGB values
painter.setBrush(QColor(255, 255, 0, 100)) # Yellow color using RGB values
else:
painter.setPen(QColor(0, 0, 255, 100)) # Blue color using RGB values
painter.setBrush(QColor(0, 0, 255, 100)) # Blue color using RGB values
painter.drawEllipse(QPoint(round(x*5.43), round(y*5.355)), 10, 10) # Draw circle
enter image description here