A
A
Alexander2020-11-15 07:54:42
PyQt
Alexander, 2020-11-15 07:54:42

How to resize a transparent window?

There is this script:

import sys
from PyQt5.QtWidgets import QWidget, QApplication, QDesktopWidget, QMessageBox
from PyQt5.QtWidgets import QTextBrowser, QGridLayout, QLabel, QCheckBox, qApp
from PyQt5.QtCore import QPoint, QObject, QThread, pyqtSignal, QRect, QSize, QTimer
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QPalette, QBrush, QImage, QCursor
from PIL import Image, ImageDraw


# Отловить ошибки в слотах PyQt5
def log_uncaught_exceptions(ex_cls, ex, tb):
    text = '{}: {}:\n'.format(ex_cls.__name__, ex)
    import traceback
    text += ''.join(traceback.format_tb(tb))

    print(text)
    QMessageBox.critical(None, 'Упс! Ошибочка...', text)
    quit()
sys.excepthook = log_uncaught_exceptions


class DisplayWindow(QWidget):
  def __init__(self):
    super().__init__()
    self.interface()
  def interface(self):
    self.setWindowFlags(Qt.Window | Qt.FramelessWindowHint)
    self.setWindowFlag(Qt.WindowStaysOnTopHint)
    self.setGeometry(200, 200, 300, 535)

    image = Image.new(mode='RGBA', size=(300, 535))

    draw = ImageDraw.Draw(image)
    draw.rectangle((0, 0, 300, 535),outline='blue', width=5)
    print(image.size[0])
    del draw

    pixmap = QPixmap(image.toqpixmap())
    palette = self.palette()
    palette.setBrush(QPalette.Normal, QPalette.Window, QBrush(pixmap))
    palette.setBrush(QPalette.Inactive, QPalette.Window, QBrush(pixmap))

    self.setPalette(palette)
    self.setMask(pixmap.mask())

    # self.setCursor(QCursor(QPixmap(cursor_image.toqpixmap())))
    self.setToolTip('Сканер')
    # self.showFullScreen()

  def drag_window(self, event):
    delta = QPoint(event.globalPos() - self.old_position)
    self.move(self.x() + delta.x(), self.y() + delta.y())
    self.old_position = event.globalPos()
  def mousePressEvent(self, event):
    """Выбрать окно при помощи мыши"""
    self.old_position = event.globalPos()
  def mouseReleaseEvent(self, event):
    print(event.x())
    if event.buttons() == Qt.LeftButton:
      print("Левая кнопка мыши отпущена")
  def mouseMoveEvent(self, event):
    """Переместить окно с помощью мыши"""
    if event.buttons() == Qt.NoButton:
      print("Simple mouse motion")
    elif event.buttons() == Qt.LeftButton:
      print("Left click drag")
      self.drag_window(event)
    elif event.buttons() == Qt.RightButton:
      print("Right click drag")
    x, y = self.get_window_coordinates()
    width, height = self.get_window_size()
    region = x, y, width, height
    super(DisplayWindow, self).mouseMoveEvent(event)
  def get_window_size(self):
    """Вернёт кортеж(width, height) размер окна"""
    size = self.frameSize().width(), self.frameSize().height()
    return size
  def get_window_coordinates(self):
    """Вернёт кортеж(x, y) координаты окна"""
    coordinates = self.x(), self.y()
    return coordinates
  def resizeEvent(self, event):
    print(event)
  def releaseMouse(self, event):
    print(event.x())
  def wheelEvent(self, event):
    print(event.angleDelta())

def main():
  application = QApplication(sys.argv)
  display_window = DisplayWindow()
  display_window.show()
  sys.exit(application.exec_())

if __name__ == '__main__':
    main()


Implementation of a transparent window with a frame.
I did the drag and drop, now I can't do the resize with the mouse.
Google didn't find anything on this topic.
Please help.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question