試圖創建餅圖形狀,但出於某種原因,我無法正確地將它連接在一起。當我運行我的代碼時,它會在彼此之上創建很多段。 這裏是我的代碼:如何在python中使用pyqt創建餅圖
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys, random
app = QApplication(sys.argv)
scene = QGraphicsScene()
families = [1,2,3,4,5,6,7]
total = 0
colours = []
set_angle = 0
count1 = 0
total = sum(families)
for count in range(len(families)):
number = []
for count in range(3):
number.append(random.randrange(0, 255))
colours.append(QColor(number[0],number[1],number[2]))
for family in families:
angle = round(family/total*16*360)
ellipse = QGraphicsEllipseItem(0,0,400,400)
ellipse.setPos(0,0)
ellipse.setStartAngle(set_angle)
ellipse.setSpanAngle(angle)
ellipse.setBrush(colours[count1])
set_angle = angle
count1 +=1
scene.addItem(ellipse)
view = QGraphicsView(scene)
view.show()
app.exec_()