python绘制透明九宫格

发布时间:2024年01月12日

主要使用的是python的pyqt5库来进行绘制,直接命令安装即可。

pip install pyqt5

?效果图如下:

直接上全部代码。?

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QTimer, QRect, Qt, QLine, QPoint
from PyQt5.QtGui import QImage, QPixmap, QPainter, QPen
from PyQt5.QtWidgets import QMessageBox, QWidget
import pyautogui
from time import sleep
class N9box(QWidget):
    def __init__(self, w,h,parent=None):
        super(N9box, self).__init__(parent)
        self.w = w
        self.h = h
        self.w_cnt = 4
        self.h_cnt = 3
        self.resize(self.w, self.h)
        self.rects = []
        self.label1 = QtWidgets.QLabel(self)
        self.label1.setGeometry(QtCore.QRect(0, 0, self.w//self.w_cnt, self.h//self.h_cnt))
        self.label1.setStyleSheet("background-color:rgba(222, 0, 0, 100);")
        #无边框与置顶
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.Tool | Qt.WindowStaysOnTopHint)
        self.setAttribute(Qt.WA_TranslucentBackground)

    def paintEvent(self, QPaintEvent):
        for i in range(3):
            for j in range(4):
                self.rects.append(QRect(j*self.w//self.w_cnt,i*self.h//self.h_cnt,
                                        self.w//self.w_cnt,self.h//self.h_cnt))
        paint = QPainter(self)
        paint.begin(self)
        paint.setPen(QPen(Qt.red,2,Qt.SolidLine))
        for rect in self.rects:
            paint.drawRect(rect)
        #paint.drawLine(0,0,self.w,self.h)
        paint1 = QPen()
        paint1.setWidth(2)
        paint1.setColor(Qt.red)
        paint.setPen(paint1)
        paint.drawPoint(1000,800)
        paint.end()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w, h = pyautogui.size()
    my = N9box(w,h)
    my.show()
    sys.exit(app.exec_())

文章来源:https://blog.csdn.net/xiaobaidaima/article/details/135532657
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。