У меня есть вот такой класс окна: class DrawingWindow(QMainWindow): def init(self,…
- User
У меня есть вот такой класс окна: class DrawingWindow(QMainWindow): def init(self, coordinates): super().init() self.setWindowTitle("Transparent Drawing Window") self.setGeometry(0, 0, QApplication.desktop().screenGeometry().width(), QApplication.desktop().screenGeometry().height()) self.setAttribute(Qt.WA_TranslucentBackground, True) self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
self.painter = QPainter() self.painter.setRenderHint(QPainter.Antialiasing) self.pen_color = QColor(255, 0, 0) # Set the initial pen color to red self.pen_width = 4 # Set the initial pen width to 4 self.coordinates = coordinates # Store the coordinates for drawing rectangles self.starting_point = None self.setFrameStyle(QFrame.Box) # Set functional frame shape def paintEvent(self, event): self.painter.begin(self) self.painter.setPen(Qt.NoPen) self.painter.setBrush(QBrush(Qt.transparent)) self.painter.drawRect(QRect(0, 0, self.width(), self.height())) # Draw a transparent background self.painter.setPen(QPen(QColor(self.pen_color), self.pen_width)) self.painter.setBrush(QBrush(Qt.transparent)) self.painter.drawRect(self.coordinates[0], self.coordinates[1], self.coordinates[2], self.coordinates[3]) # Draw rectangles using the provided coordinates self.painter.end() def mousePressEvent(self, event): self.starting_point = event.pos() def mouseMoveEvent(self, event): if self.starting_point: current_point = event.pos() if current_point.x() > self.coordinates[0] + self.coordinates[2]: self.coordinates[2] = current_point.x() - self.coordinates[0] if current_point.y() > self.coordinates[1] + self.coordinates[3]: self.coordinates[3] = current_point.y() - self.coordinates[1] if current_point.x() < self.coordinates[0] + self.coordinates[2]: self.coordinates[2] += self.coordinates[0] - current_point.x() self.coordinates[0] = current_point.x() if current_point.y() < self.coordinates[1] + self.coordinates[3]: self.coordinates[3] += self.coordinates[1] - current_point.y() self.coordinates[1] = current_point.y() self.update() self.ending_point = event.pos() def mouseReleaseEvent(self, event): '''if self.starting_point : #self.coordinates.append((x, y, width, height)) if self.ending_point.x() > self.coordinates[0] + self.coordinates[2]: self.coordinates[2] = self.ending_point.x() - self.coordinates[0] if self.ending_point.y() > self.coordinates[1] + self.coordinates[3]: self.coordinates[3] = self.ending_point.y() - self.coordinates[1] if self.ending_point.x() < self.coordinates[0] + self.coordinates[2]: self.coordinates[2] += self.coordinates[0] - self.ending_point.x() self.coordinates[0] = self.ending_point.x() if self.ending_point.y() < self.coordinates[1] + self.coordinates[3]: self.coordinates[3] += self.coordinates[1] - self.ending_point.y() self.coordinates[1] = self.ending_point.y() self.update()''' self.starting_point = NoneКак добавить к нему кнопки?
- Assistant
Для добавления кнопок к вашему классу окна DrawingWindow, вы можете использовать класс QPushButton из библиотеки PyQt5. Ниже приведен пример добавления двух кнопок - одной красной и одной синей, которые меняют цвет пера при нажатии:
from PyQt5.QtWidgets import QPushButton class DrawingWindow(QMainWindow): def __init__(self, coordinates): super().__init__() self.setWindowTitle("Transparent Drawing Window") self.setGeometry(0, 0, QApplication.desktop().screenGeometry().width(), QApplication.desktop().screenGeometry().height()) self.setAttribute(Qt.WA_TranslucentBackground, True) self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint) self.painter = QPainter() self.painter.setRenderHint(QPainter.Antialiasing) self.pen_color = QColor(255, 0, 0) # Set the initial pen color to red self.pen_width = 4 # Set the initial pen width to 4 self.coordinates = coordinates # Store the coordinates for drawing rectangles self.starting_point = None self.setFrameStyle(QFrame.Box) # Set functional frame shape red_button = QPushButton('Red', self) red_button.clicked.connect(self.set_red_color) red_button.move(10, 10) blue_button = QPushButton('Blue', self) blue_button.clicked.connect(self.set_blue_color) blue_button.move(80, 10) def set_red_color(self): self.pen_color = QColor(255, 0, 0) self.update() def set_blue_color(self): self.pen_color = QColor(0, 0, 255) self.update() def paintEvent(self, event): # реализация метода paintEvent как в вашем примере # остальной код вашего класса DrawingWindow как в вашем примереВ приведенном коде создаются две кнопки с текстом "Red" и "Blue", которые при нажатии меняют цвет пера на красный и синий соответственно. Методы set_red_color и set_blue_color отвечают за изменение цвета пера и вызов метода update для перерисовки окна.
- User
Следующий код немного баганый. Подправь пожалуйста: def mousePressEvent(self, event): self.starting_point = event.pos()
def mouseMoveEvent(self, event): if self.starting_point: current_point = event.pos() if current_point.x() > self.coordinates[0] + self.coordinates[2]: self.coordinates[2] = current_point.x() - self.coordinates[0] if current_point.y() > self.coordinates[1] + self.coordinates[3]: self.coordinates[3] = current_point.y() - self.coordinates[1] if current_point.x() < self.coordinates[0] + self.coordinates[2]: self.coordinates[2] += self.coordinates[0] - current_point.x() self.coordinates[0] = current_point.x() if current_point.y() < self.coordinates[1] + self.coordinates[3]: self.coordinates[3] += self.coordinates[1] - current_point.y() self.coordinates[1] = current_point.y() self.update() def mouseReleaseEvent(self, event): self.starting_point = None - Assistant
Попробуйте исправить методы mouseMoveEvent и mouseReleaseEvent следующим образом:
def mouseMoveEvent(self, event): if self.starting_point: current_point = event.pos() new_x = min(self.starting_point.x(), current_point.x()) new_y = min(self.starting_point.y(), current_point.y()) new_width = abs(self.starting_point.x() - current_point.x()) new_height = abs(self.starting_point.y() - current_point.y()) self.coordinates = [new_x, new_y, new_width, new_height] self.update() def mouseReleaseEvent(self, event): self.starting_point = NoneВ данном случае мы используем начальную точку (self.starting_point) и текущую точку (current_point) для определения новых координат прямоугольника. Также исправлена проверка условий для обновления координат и вызов метода update для перерисовки окна.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.