Tkinter饼图存在一些问题

时间:2019-07-26 09:19:30

标签: python tkinter pie-chart

输入

from tkinter import *
from tkinter import ttk
from tkinter.scrolledtext import ScrolledText
import requests
import json
import csv
from datetime import datetime, timedelta
import dateutil.parser
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure

def text_one():
    txt1.delete('0.0', END)

    #A lot of code that creates all the stuff that I've blurred out, that code is running fine.
    #That code is running fine, so for simplicity I've removed it from here.

    new_york = float("100") 
    paris = float("80") 
    london = float("60")
    titan = float("40")
    brooklyn = float("20")

    figure2 = Figure(figsize=(4.2,4), dpi=100) 
    subplot2 = figure2.add_subplot(111) 
    labels2 = 'New York', 'Paris', 'London', 'Titan', 'Brooklyn'
    pieSizes = [float(new_york),float(paris),float(london),float(titan), float(brooklyn)]
    explode2 = (0, 0, 0, 0, 0)  
    subplot2.pie(pieSizes, explode=explode2, labels=labels2, autopct='%1.1f%%', shadow=True, startangle=90) 
    subplot2.axis('equal')  
    pie2 = FigureCanvasTkAgg(figure2, txt1) 
    pie2.get_tk_widget().pack(anchor=tk.E)

def update():
    text_one()
    window.after(1000 * 60 * 1, update)

window = Tk()
window.geometry("1178x1080")
tab_control = ttk.Notebook(window)
tab1 = ttk.Frame(tab_control)
tab2 = ttk.Frame(tab_control)
tab3 = ttk.Frame(tab_control)
tab_control.add(tab1, text='Wallet')
tab_control.add(tab2, text='Inventory Quantity')
tab_control.add(tab3, text='Inventory Alphabetical')
txt1 = (ScrolledText(tab1))
txt1.pack(fill=BOTH, expand=True)
txt2 = (ScrolledText(tab2))
txt2.pack(fill=BOTH, expand=True)
txt3 = (ScrolledText(tab3))
txt3.pack(fill=BOTH, expand=True)
update()
tab_control.pack(expand=1, fill='both')
window.mainloop()

这是运行3分钟后的输出。 enter image description here 第一个问题是3,当它第一次运行时,只有一个按照我想要的方式在顶部运行,然后当“ window.after(1000 * 60 * 1,update)”更新了东西时,第二个问题就不存在了。 ,不久将紧随其后。 txt1.delete('0.0',END)曾用于在写入新数据之前清除所有其他数据,但似乎对饼图没有任何影响。

那我怎么才能只刷新右上角的单个饼图?

在目前仅是一个演示的情况下,我最终打算使它动态地显示饼图形式的模糊信息。

另一个问题是,您会注意到右侧的垂直滚动条,如果我将其上下滚动,饼图的位置完全不变,我希望将它们绑定到画布的其余部分也是如此。

1 个答案:

答案 0 :(得分:0)

基本上,您只需要更新相同的图即可。您当前的设置会一遍又一遍地创建新图。您可以做的是:

num = [float(randint(30,100)) for _ in range(5)] #your method to retrieve new data
subplot2.clear()
subplot2.pie(num, explode=self.explode2, labels=self.labels2, autopct='%1.1f%%', shadow=True, startangle=90)
pie2.draw_idle()

要在您的代码中实现此目的,建议创建一个类来容纳所有这些内容:

from tkinter import *
from tkinter import ttk
from tkinter.scrolledtext import ScrolledText
from random import randint
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure

class Graph:
    def __init__(self):
        txt1.delete('0.0', END)

        new_york = float("100")
        paris = float("80")
        london = float("60")
        titan = float("40")
        brooklyn = float("20")

        self.figure2 = Figure(figsize=(4.2,4), dpi=100)
        self.subplot2 = self.figure2.add_subplot(111)
        self.labels2 = 'New York', 'Paris', 'London', 'Titan', 'Brooklyn'
        self.pieSizes = [float(new_york),float(paris),float(london),float(titan), float(brooklyn)]
        self.explode2 = (0, 0, 0, 0, 0)
        self.subplot2.pie(self.pieSizes, explode=self.explode2, labels=self.labels2, autopct='%1.1f%%', shadow=True, startangle=90)
        self.subplot2.axis('equal')
        self.pie2 = FigureCanvasTkAgg(self.figure2, txt1)
        self.pie2.get_tk_widget().pack(anchor=tk.E)

    def update(self):
        num = [float(randint(30,100)) for _ in range(5)]
        self.subplot2.clear()
        self.subplot2.pie(num, explode=self.explode2, labels=self.labels2, autopct='%1.1f%%', shadow=True, startangle=90)
        self.pie2.draw_idle()
        window.after(1000, self.update)

window = Tk()
window.geometry("800x600")
tab_control = ttk.Notebook(window)
tab1 = ttk.Frame(tab_control)
tab2 = ttk.Frame(tab_control)
tab3 = ttk.Frame(tab_control)
tab_control.add(tab1, text='Wallet')
tab_control.add(tab2, text='Inventory Quantity')
tab_control.add(tab3, text='Inventory Alphabetical')
txt1 = (ScrolledText(tab1))
txt1.pack(fill=BOTH, expand=True)
txt2 = (ScrolledText(tab2))
txt2.pack(fill=BOTH, expand=True)
txt3 = (ScrolledText(tab3))
txt3.pack(fill=BOTH, expand=True)
a = Graph()
a.update()
tab_control.pack(expand=1, fill='both')
window.mainloop()
相关问题