我在使用 Tkinter 饼图时遇到的一些问题
A few problems I'm having with the Tkinter piechart
输入
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 分钟后的输出。
第一个问题是有 3 个,当它第一次 运行 时只有一个在上面,这是我想要的方式,然后当 "window.after(1000 * 60 * 1, update)" 更新东西时,下面有第二个,很快就会被跟踪3日。
txt1.delete('0.0', END) 用于在写入新数据之前删除所有其他数据,但它似乎对饼图没有任何影响。
那么我怎样才能让它只刷新右上角的单个饼图呢?
万一它是相关的,它现在只是一个演示,我最终打算让它动态地显示饼图形式的模糊信息。
另一个问题是你会注意到右边的垂直滚动条,如果我从上到下降低它,饼图的位置根本不会改变,我希望它们是以与其余部分相同的方式绑定到 canvas。
基本上你只需要更新同一个情节。您当前的设置一遍又一遍地创建一个新的情节。你可以做的是:
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()
为了在您的代码中实现此功能,我建议创建一个 class 来保存所有这些:
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()
输入
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 分钟后的输出。
那么我怎样才能让它只刷新右上角的单个饼图呢?
万一它是相关的,它现在只是一个演示,我最终打算让它动态地显示饼图形式的模糊信息。
另一个问题是你会注意到右边的垂直滚动条,如果我从上到下降低它,饼图的位置根本不会改变,我希望它们是以与其余部分相同的方式绑定到 canvas。
基本上你只需要更新同一个情节。您当前的设置一遍又一遍地创建一个新的情节。你可以做的是:
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()
为了在您的代码中实现此功能,我建议创建一个 class 来保存所有这些:
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()