python - 试图用迭代循环制作多个饼图,只得到一个
问题描述
我正在尝试使用以下代码制作多个饼图:
n = 0
perc = list()
while n < len(piedata):
perc.append(piedata[n+2])
n += 3
print (perc)
n = 0
fails = list()
while n < len(piedata):
fails.append(piedata[n+1])
n += 3
print(fails)
n = 0
titles = list()
while n < len(piedata):
titles.append(piedata[n])
n += 3
print(titles)
for percent, fail, title in zip(perc, fails, titles):
piedata = [percent, (100-int(percent))]
fig = matplotlib.figure.Figure(figsize=(5, 5))
ax = fig.add_subplot(111)
ax.pie(piedata) # this is the information that the circle diagram will be made out of
ax.legend([('amount of attempts:', NOTT), ('amount of fails', fail)])
circle = matplotlib.patches.Circle((0, 0), 0.7, color='white')
ax.add_artist(circle)
# this is the code for actually putting the circle diagram/pie chart on the screen
canvas = FigureCanvasTkAgg(fig, master=window)
canvas.get_tk_widget().pack()
canvas.draw()
Label(window, text=(title, title), bg='light blue').pack()
window.mainloop()
print(percent)
print(fail)
据我所知,我的问题是最后window.mainloop()
只能使用一次,然后不能再次使用。
我目前正在尝试从中获得 2 个循环。
解决方案
对于任何发现这一点的人:tk.mainloop()(在我的例子中是 window.mainloop())在许多情况下都会停止程序表单的运行。这是它的一个非常简化的版本,但如果你遇到过这个问题,答案可能是将其替换为:
window.update_idletasks()
window.update()
但是,如果您像我一样在循环中使用它,则需要添加一个 break 子句或使用 for 循环,否则它将继续循环。
推荐阅读
- python - 在peewee中跨多个数据库使用多个M2M关系
- coldfusion - Adobe Coldfusion 错误:找不到应用程序。null
错误发生在-1行 - c# - 为什么ubuntu中的NET Core 3.1 BackgroundWorker不能访问环境变量?
- android - 无法弄清楚如何让协程正常工作以将数据插入 Room 数据库
- regex - 用于根据以下正则表达式验证多个输入的正则表达式
- c# - 在 .net core 3x 中,Regex Match,Groups 不能输入到 Enumerable extensions
- httpclient - 在 spfx webpart 中使用 SPHttpClient 将文件上传到 SharePoint Online,并带有相应的列数据
- ruby - 阵列形成和定制
- java - 如何对新创建的约会做出反应?
- css - 为什么 ant design 不提供像 bootstrap 这样的 CSS 文档?