我和特金特·皮奇有一些问题

2024-04-20 10:14:27 发布

您现在位置:Python中文网/ 问答频道 /正文

输入

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个,当它第一次运行时,只有一个在顶部,我想要的方式,然后当窗口.after(1000*60*1,更新)“更新的东西,在Neigh下面有第二个,很快就会有第三个。 delete('0.0',END)用于在写入新数据之前除去所有其他数据,但它似乎对piechart没有任何影响。你知道吗

所以我怎样才能让它只刷新右上角的一个饼图呢?你知道吗

如果它是相关的,现在只是一个演示,我最终打算让它动态显示的信息,已经模糊了饼图的形式。你知道吗

另一个问题是,你会注意到右边的垂直滚动条,如果我从上到下降低它,饼图的位置一点也不改变,我希望它们和画布上的其他部分一样。你知道吗


Tags: textfromimportaddtruetkinterfloatwindow
1条回答
网友
1楼 · 发布于 2024-04-20 10:14:27

基本上你只需要更新相同的情节。当前设置会反复创建新的绘图。你能做的是:

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()

相关问题 更多 >