# -*- coding=utf-8 -*-
# from PyQt5.QtWidgets import QMainWindow, QApplication
# from PyQt5.QtCore import *
# from PyQt5.QtGui import *
# from PyQt5.QtWidgets import *
# from PyQt5.QtWidgets import QMainWindow, QApplication
# import sys
# from PyQt5.QtWidgets import QWidget
# class MainWindow(QWidget):
# def __init__(self):
# super().__init__()
# self.setObjectName("MainWindow")
# self.setStyleSheet("#MainWindow{border-image: url(77777.png);}") # 这里使用相对路径,也可以使用绝对路径
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QPainter,QPixmap
from PyQt5.QtCore import Qt
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import QMainWindow, QApplication
class Winform(QWidget):
def __init__(self, parent=None):
super(Winform, self).__init__(parent)
self.setWindowTitle("paintEvent设置背景颜色")
self.resize(700, 500)
def paintEvent(self, event):
painter = QPainter(self)
# todo 1 设置背景颜色
# painter.setBrush(Qt.green)
# painter.drawRect(self.rect())
# todo 2 设置背景图片,平铺到整个窗口,随着窗口改变而改变
# pixmap = QPixmap("./images/77777.png")
pixmap = QPixmap("123123.bmp") # 本程序同文件夹下设置背景路径
painter.drawPixmap(self.rect(), pixmap)
# def paintEvent(self, QPaintEvent):
"""坐标轴"""
X = ["当前设备占用状态"] # 横坐标
Y = [141, 110, 120, 130, 140, 150, 160] # 纵坐标
painter = QPainter() # 画刷
painter.begin(self) # 开始画
painter.setPen(Qt.NoPen)
painter.setRenderHint(QPainter.Antialiasing) # 抗锯齿
# Qpen 坐标轴颜色 (red红,blue蓝,green绿,yellow黄,white白,black黑,gray灰,
# Qpen https://www.cnblogs.com/msgarden/p/4949272.html设置颜色 https://www.xuebuyuan.com/1085263.html设置样式
pen = QPen(Qt.white, 4, Qt.SolidLine) # 设置坐标轴颜色,坐标轴线条宽度,画出的线的类型
painter.setPen(pen) # painter使用的画线方法为QPen设置的属性
pointx = 35 # x轴坐标
pointy = self.height() - 50 # y轴坐标
width = self.width() - 60 - pointx # 横坐标长度
height = self.height() - 100 # 纵坐标长度
painter.drawLine(pointx, pointy, width + pointx, pointy) # X轴
painter.drawLine(pointx, pointy - height, pointx, pointy) # Y轴
X_Spacing = width / len(X) # X间距 = 宽度/长度
# X_little_Spacing = width / len(X) / (len(Y[1]) + 1) # 小间距
X_little_Spacing = width / len(X) / (len(Y) + 1) # 小间距
Y_Spacing = height / len(Y) # Y间距 = 高度/长度
pen = QPen(Qt.red, 5, Qt.SolidLine) # 设置颜色
painter.setPen(pen) # painter使用的画线方法为QPen设置的属性
X_start = X_little_Spacing * 3 + 35 # X开始位置 见36行
X_pox = []
for i in range(len(X)):
painter.drawPoint(X_start - X_little_Spacing * 2, pointy)
painter.drawPoint(X_start - X_little_Spacing, pointy)
painter.drawPoint(X_start, pointy)
painter.drawPoint(X_start + X_little_Spacing * 2, pointy)
painter.drawText(X_start - 15, pointy + 20, str(X[i]))
X_pox.append(X_start)
X_start = X_start + X_Spacing
Y_max = 0
for i in range(len(Y)):
# if Y_max < max(Y[i]):
# Y_max = max(Y[i])
pass
num_Spacing = int(Y_max / (len(Y) - 1)) + 1
num_start = 0
Y_start_pox = 0
end_Pox = 0
for i in range(len(Y)):
painter.drawPoint(35, pointy - Y_start_pox)
end_Pox = pointy - Y_start_pox
painter.drawText(pointx - 30, pointy - Y_start_pox + 5, str(num_start))
Y_start_pox = Y_start_pox + Y_Spacing
num_start = num_start + num_Spacing
every_pox = (pointy - end_Pox) / (num_start - num_Spacing)
pen = QPen(Qt.black, 1, Qt.SolidLine)
painter.setPen(pen)
print(X_pox)
for i in range(len(X)):
# brush = QBrush(Qt.Dense1Pattern)
brush = QBrush(Qt.Dense1Pattern)
painter.setBrush(brush)
# Qbrush.setStyleSheet("color:blue")
# QBrush.color(0, 0, 0)
painter.setBrush(QColor(192, 253, 123)) # 设置柱状图颜色
# painter.drawText(X_pox[i] - X_little_Spacing * 2, pointy - Y[i] * every_pox - 5, str(Y[i]))
painter.drawText(47, pointy - Y[i] + 2, str("123")) # 在直方柱上写数值
# painter.drawRect(X_pox[i] - X_little_Spacing * 2, pointy - Y[i] * every_pox, X_little_Spacing,Y[i] * every_pox)
painter.drawRect(100, pointy - Y[i], 20, 140) # 画矩形,drawRect(X,Y,W,H)
brush = QBrush(Qt.Dense1Pattern)
painter.setBrush(brush)
painter.setBrush(QColor(255, 255, 0)) # 设置柱状图颜色
# painter.drawText(X_pox[i] - X_little_Spacing * 2, pointy - Y[i] * every_pox - 5, str(Y[i]))
painter.drawText(67, 202, str("321")) # 在直方柱上写数值
# painter.drawRect(X_pox[i] - X_little_Spacing * 2, pointy - Y[i] * every_pox, X_little_Spacing,Y[i] * every_pox)
painter.drawRect(140, 279, 20, 170) # 画矩形,drawRect(X,Y,W,H)
brush = QBrush(Qt.Dense1Pattern)
painter.setBrush(brush)
painter.setBrush(QColor(0, 255, 0)) # 设置柱状图颜色
painter.drawText(87, 202, str("321")) # 在直方柱上写数值
painter.drawRect(180, 49, 20, 400) # 画矩形,drawRect(X,Y,W,H)
# painter.drawText(X_pox[i] - X_little_Spacing * 2, pointy - Y[i][0] * every_pox - 5, str(Y[i][0]))
# painter.drawRect(X_pox[i] - X_little_Spacing * 2, pointy - Y[i][0] * every_pox, X_little_Spacing,
# Y[i][0] * every_pox)
#
# brush = QBrush(Qt.DiagCrossPattern)
# painter.setBrush(brush)
# # painter.setBrush(QColor(65, 105, 225)) # 设置柱状图颜色
# painter.drawText(X_pox[i] - X_little_Spacing, pointy - Y[i][1] * every_pox - 5, str(Y[i][1]))
# painter.drawRect(X_pox[i] - X_little_Spacing, pointy - Y[i][1] * every_pox, X_little_Spacing,
# Y[i][1] * every_pox)
#
# brush = QBrush(Qt.Dense2Pattern)
# painter.setBrush(brush)
# # painter.setBrush(QColor(255, 69, 0)) # 设置柱状图颜色
# painter.drawText(X_pox[i], pointy - Y[i][2] * every_pox - 5, str(Y[i][2]))
# painter.drawRect(X_pox[i], pointy - Y[i][2] * every_pox, X_little_Spacing, Y[i][2] * every_pox)
#
# brush = QBrush(Qt.Dense3Pattern)
# painter.setBrush(brush)
# # painter.setBrush(QColor(255, 255, 0)) # 设置柱状图颜色
# painter.drawText(X_pox[i] + X_little_Spacing, pointy - Y[i][3] * every_pox - 5, str(Y[i][3]))
# painter.drawRect(X_pox[i] + X_little_Spacing, pointy - Y[i][3] * every_pox, X_little_Spacing,
# Y[i][3] * every_pox)
# 柱状图名称(每种颜色代表含义,右上角)
# brush = QBrush(Qt.Dense1Pattern)
# painter.setBrush(brush)
# painter.drawRect(self.width() - 140, 10, 50, 20)
# painter.drawText(self.width() - 80, 25, "Dense1Pattern")
#
# brush = QBrush(Qt.DiagCrossPattern)
# painter.setBrush(brush)
# painter.drawRect(self.width() - 140, 35, 50, 20)
# painter.drawText(self.width() - 80, 50, "DiagCrossPattern")
#
# brush = QBrush(Qt.Dense2Pattern)
# painter.setBrush(brush)
# painter.drawRect(self.width() - 140, 60, 50, 20)
# painter.drawText(self.width() - 80, 75, "Dense2Pattern")
#
# brush = QBrush(Qt.Dense3Pattern)
# painter.setBrush(brush)
# painter.drawRect(self.width() - 140, 85, 50, 20)
# painter.drawText(self.width() - 80, 100, "Dense3Pattern")
""" """
if __name__ == '__main__':
app = QApplication(sys.argv)
# window = QMainWindow()
# window.setObjectName("MainWindow")
# window.setStyleSheet("#MainWindow{background-image: url(123123.bmp);}") # 这里使用相对路径,也可以使用绝对路径
# window.show()
# sys.exit(app.exec_())
demo = Winform()
# demo = MainWindows()
# demo.setObjectName("MainWindow")
# demo.setStyleSheet("#MainWindow{background-image: url(123123.bmp);}")
# bj = QPalette()
# bj.setBrush(QPalette.Background, QBrush(QPixmap("/images/123123.bmp")))
# demo.setObjectName("MainWindow")
# demo.setStyleSheet("#MainWindow{background-image: url(123123.bmp);}")
demo.show()
# demo.setAutoFillBackground(True)
sys.exit(app.exec_())
# def __init__(self):
# super().__init__()
# self.resize(700, 500)
# self.setObjectName("MainWindow")
# self.setStyleSheet("#MainWindow{border-image: url(77777.png);}") # 这里使用相对路径,也可以使用绝对路径
# if __name__ == '__main__':
# app = QApplication(sys.argv)
# window = MainWindow()
# # window.setObjectName("MainWindow")
# # window.setStyleSheet("#MainWindow{border-image: url(77777.png);}") # 这里使用相对路径,也可以使用绝对路径
# window.show()
# sys.exit(app.exec_())