gpt4 book ai didi

python - 弧形中的不同颜色

转载 作者:行者123 更新时间:2023-12-04 01:23:43 25 4
gpt4 key购买 nike

考虑以下玩具示例:

from PyQt5 import QtWidgets, QtGui, QtCore

class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()

w = QtWidgets.QWidget()
layout = QtWidgets.QVBoxLayout()
w.setLayout(layout)
self.setCentralWidget(w)

label = QtWidgets.QLabel()
canvas = QtGui.QPixmap(400, 300)
label.setPixmap(canvas)
layout.addWidget(label)

def paintEvent():
painter = QtGui.QPainter(label.pixmap())
painter.setRenderHint(QtGui.QPainter.Antialiasing)
painter.setPen(QtCore.Qt.red)
painter.drawArc(0, 0, 100, 100, 1440, -2880)
painter.end()

paintEvent()

self.show()

app = QtWidgets.QApplication([])
window = MainWindow()
app.exec_()

我怎样才能使用任意数量的颜色(最好是不同长度的)来绘制弧线?

我尝试用渐变(线性和圆锥形)来做,但我一直无法获得准确的结果。

我想更广泛的问题是,在绘制弧线时,我能否以某种方式使用不同的笔颜色?请注意,圆弧可以是半圆、整圆或介于两者之间的任何形状。

颜色将使用百分比分布。每种颜色都是弧长的一部分。但我对所有颜色均等间距的解决方案感到满意。

最佳答案

一个可能的解决方案是分段绘制圆弧:

from PyQt5 import QtCore, QtGui, QtWidgets


class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()

w = QtWidgets.QWidget()
layout = QtWidgets.QVBoxLayout()
w.setLayout(layout)
self.setCentralWidget(w)

label = QtWidgets.QLabel()
canvas = QtGui.QPixmap(400, 300)
canvas.fill(QtGui.QColor("white"))
label.setPixmap(canvas)
layout.addWidget(label)

def paint_label():
painter = QtGui.QPainter(label.pixmap())
painter.setRenderHint(QtGui.QPainter.Antialiasing)
r = QtCore.QRect(0, 0, 100, 100)
delta_angle = -180 * 16
start_angle = 90 * 16
values = (1, 2, 3, 4)
colors = (
QtGui.QColor("red"),
QtGui.QColor("blue"),
QtGui.QColor("green"),
QtGui.QColor("yellow"),
)
sum_of_values = sum(values)
for value, color in zip(values, colors):
end_angle = start_angle + int((value/sum_of_values) * delta_angle)
painter.setPen(color)
painter.drawArc(r, start_angle, end_angle - start_angle)
start_angle = end_angle
painter.end()

paint_label()

self.show()


def main():

app = QtWidgets.QApplication([])
window = MainWindow()
app.exec_()


if __name__ == "__main__":
main()

关于python - 弧形中的不同颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62216380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com