Код: Выделить всё
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()
Я пытался изменить значения x и y, но после внесения этих изменений синие, желтые и зеленые круги исчез
Код: Выделить всё
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
Подробнее здесь: https://stackoverflow.com/questions/787 ... rectpython