Input
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()
And here's the output after 3 mins of running.
The first problem is there's 3, when it first ran there was just one up the top, the way I wanted it, then when "window.after(1000 * 60 * 1, update)" updated things, There was a second one underneigh, soon to be followed by a 3rd.
txt1.delete('0.0', END) used to get rid of all the other data before new data was written in, but it doesn't seem to have any effect on the piechart.
So how can I have it refresh just the single piechart in the top right?
Incase it's relevant it's just a demo for now, I eventually intend to make it dynamically display the information that's been blurred out in pie chart form.
Another issue is that you'll notice the vertical scroll bar on the right, if I lower that from top to bottom, the positions of the pie charts don't change at all, I'm wanting them to be tied to the canvas the same way the rest of it is.
Basically you just need to update the same plot. You current setup creates a new plot over and over. What you could do is:
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()
To make this work in your code, I suggest creating a class to hold all these:
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()