如何在TKINTER中清除/刷新数字

发布于 2025-01-23 08:48:49 字数 3097 浏览 1 评论 0原文

我正在尝试设计应用程序的用户界面。该应用程序将使用可靠性fitter向用户显示一些图表。

使用可靠性的问题

很难在tkinter上显示图形,而无需不断弹出mini图作为可靠性使用matplotlib.pyplot < /代码>。

接下来,主要问题是在tkinter中绘制图形时。单击按钮时,我希望它更改为另一个图。但是到目前为止,到目前为止,我尝试的是毫无意义的,该图将全部堆叠起来。我尝试了.get_tk_widget.delete()和其他帖子中提到的方法,但它不会删除tkinter上的图形。 plt.clf()在删除弹出的图形但未在tkinter中的图形上。

graph堆叠

这是我所做的

以下代码是我试图尝试的简单版本设计。如果有人有兴趣,完整的代码为在这里

import tkinter as tk
import pandas as pd
import numpy as np
import matplotlib.pyplot
import matplotlib.pyplot as plt 
matplotlib.use("TkAgg")

from tkinter import ttk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
from reliability.Fitters import Fit_Weibull_3P
from reliability.Probability_plotting import plot_points

main_data= pd.ExcelFile(r'C:\Users\Documents\All data.xlsx')
df= pd.read_excel(main_data, '124 deg' , names= ['RSR 100', 'BS'])
df1 = pd.read_excel(main_data, '154 deg', names= ['RSR 100', 'BS'])
df.index=np.arange(1,len(df['BS'])+1)
df1.index=np.arange(1,len(df1['BS'])+1)
data1= abs(df.loc[:, 'BS'].to_numpy())
data2= abs(df1.loc[:, 'BS'].to_numpy())

def plot_graph(): 
    fig = plt.figure(figsize = (5, 5), dpi = 100)
    fit_distributions = Fit_Weibull_3P(failures= data1, show_probability_plot= False, print_results= False)
    plt1 = fig.add_subplot(111) #subplot here is to make the pyplot to be embed on tkinter.
    fit_distributions.distribution.CDF(label='Fitted Distribution', color='Steelblue')
    plot_points(failures=data1, func='CDF', label='Failure data', color='red', alpha= 0.7)
    plt.legend() 
    canvas = FigureCanvasTkAgg(fig, master = window)  
    canvas.draw()
    canvas.get_tk_widget().pack()

def plot_next_graph():
    fig = plt.figure(figsize = (5, 5), dpi = 100)
    fit_distributions = Fit_Weibull_3P(failures= data2, show_probability_plot= False, print_results= False)
    plt1 = fig.add_subplot(111)
    fit_distributions.distribution.CDF(label='Fitted Distribution', color='Steelblue')
    plot_points(failures=data2, func='CDF', label='Failure data', color='red', alpha= 0.7)
    plt.legend()
    plt.close()
    canvas = FigureCanvasTkAgg(fig, master = window)  
    canvas.draw()
    canvas.get_tk_widget().pack()

window = tk.Tk()
window.title('Plotting in Tkinter')
window.geometry("500x500")
plot_button1 = ttk.Button(master = window, command = plot_graph, width = 15, text = "Plot 124 deg")
plot_button1.pack()
plot_button2 = ttk.Button(master = window, command = plot_next_graph, width = 15, text = "Plot 154 deg")
plot_button2.pack()
window.mainloop()

我仍然是pythontkinter的初学者,如果有人会花时间来帮助和指导我,那么会很感激。谢谢。

I'm trying to design a user interface for an app. The app will be using reliability and fitter to show the user some graphs.

Problems

Using reliability is difficult to show the graph on tkinter without having a mini figure popping out constantly as reliability use matplotlib.pyplot.

Next, the main problem, when plotting the graph in tkinter. I want it to change to another graph when I clicked on the button. But what I tried so far is pointless so far, the graph will be all stacked up. I have tried .get_tk_widget.delete() and methods mention in other posts but it won't remove the graph that is on tkinter. plt.clf() on remove the figure that pops up but not on the ones in tkinter.

Graph stacking

Here is what I have done

The below code is a simple version of what I trying to design. If anyone is interested, the full code is here.

import tkinter as tk
import pandas as pd
import numpy as np
import matplotlib.pyplot
import matplotlib.pyplot as plt 
matplotlib.use("TkAgg")

from tkinter import ttk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
from reliability.Fitters import Fit_Weibull_3P
from reliability.Probability_plotting import plot_points

main_data= pd.ExcelFile(r'C:\Users\Documents\All data.xlsx')
df= pd.read_excel(main_data, '124 deg' , names= ['RSR 100', 'BS'])
df1 = pd.read_excel(main_data, '154 deg', names= ['RSR 100', 'BS'])
df.index=np.arange(1,len(df['BS'])+1)
df1.index=np.arange(1,len(df1['BS'])+1)
data1= abs(df.loc[:, 'BS'].to_numpy())
data2= abs(df1.loc[:, 'BS'].to_numpy())

def plot_graph(): 
    fig = plt.figure(figsize = (5, 5), dpi = 100)
    fit_distributions = Fit_Weibull_3P(failures= data1, show_probability_plot= False, print_results= False)
    plt1 = fig.add_subplot(111) #subplot here is to make the pyplot to be embed on tkinter.
    fit_distributions.distribution.CDF(label='Fitted Distribution', color='Steelblue')
    plot_points(failures=data1, func='CDF', label='Failure data', color='red', alpha= 0.7)
    plt.legend() 
    canvas = FigureCanvasTkAgg(fig, master = window)  
    canvas.draw()
    canvas.get_tk_widget().pack()

def plot_next_graph():
    fig = plt.figure(figsize = (5, 5), dpi = 100)
    fit_distributions = Fit_Weibull_3P(failures= data2, show_probability_plot= False, print_results= False)
    plt1 = fig.add_subplot(111)
    fit_distributions.distribution.CDF(label='Fitted Distribution', color='Steelblue')
    plot_points(failures=data2, func='CDF', label='Failure data', color='red', alpha= 0.7)
    plt.legend()
    plt.close()
    canvas = FigureCanvasTkAgg(fig, master = window)  
    canvas.draw()
    canvas.get_tk_widget().pack()

window = tk.Tk()
window.title('Plotting in Tkinter')
window.geometry("500x500")
plot_button1 = ttk.Button(master = window, command = plot_graph, width = 15, text = "Plot 124 deg")
plot_button1.pack()
plot_button2 = ttk.Button(master = window, command = plot_next_graph, width = 15, text = "Plot 154 deg")
plot_button2.pack()
window.mainloop()

I am still a beginner in Python and tkinter, would appreciate if anyone would spend their time to help and guide me. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文