Hello all. I am new to this forum and to Python. I am writing a simple code to make a prototype demonstrator running on a Raspberry and need to draw a rectangle when the left button of the mouse is pressed.
I am using PyQt5 as I thought it would be easy to handle as the library has all I need, but I am sure I am being foolish.
Here is the code I “wrote”:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QAction, QLabel, QMainWindow
from PyQt5.QtGui import QPainter, QPixmap, QPen, QBrush, QImage, QIcon
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtCore import Qt, QPoint
import time
image_path = r"/home/pi/Pictures/SCREEN2.jpg"
class Window(QMainWindow):
def init(self):
super().init()
self.image = QPixmap(image_path)
self.showFullScreen()
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
pen = QtGui.QPen()
pen.setWidth(3)
pen.setColor(QtGui.QColor(255, 0, 0))
brush = QtGui.QBrush()
brush.setColor(QtGui.QColor(255, 0, 0))
brush.setStyle(Qt.SolidPattern)
painter.setBrush(brush)
painter.setPen(pen)
painter.drawRect(207, 152, 409, 222)
painter.end()
if name == “main”:
app=QApplication(sys.argv)
window = Window()
window.show()
app.exec()
Is there anybody with a shiny soul that can help me with this?
If this message is not correctly placed in the forum, please let me know and I will move it ASAP.
Thank you all for watching.
Joan.