tkinter展示清除matplotlib图

在这里插入图片描述

from tkinter import *
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg,
                                               NavigationToolbar2Tk)


def plot():
    global output, fig
    if fig:
        fig.clear()

    fig = Figure(figsize=(5, 5), dpi=100)
    y = [i ** 2 for i in range(101)]
    # adding the subplot
    plot1 = fig.add_subplot(111)

    # plotting the graph
    plot1.plot(y)

    # creating the Tkinter canvas
    # containing the Matplotlib figure
    if output:
        output.get_tk_widget().destroy()
    output = FigureCanvasTkAgg(fig, master=canvas)
    output.draw()

    # placing the canvas on the Tkinter window
    output.get_tk_widget().pack()


def clear_plot():
    global output
    print(fig)
    if output:
        for child in canvas.winfo_children():
            child.destroy()
        # or just use canvas.winfo_children()[0].destroy()
    output = None

# 这样耶OK
def clear_plot_V2():
    global output

    if output:
        output.get_tk_widget().destroy()
    output = None

# the main Tkinter window
window = Tk()

output = None
fig = None

# setting the title
window.title('Plotting in Tkinter')

# dimensions of the main window
window.geometry("700x700")

canvas = Canvas(window, width=500, height=500, bg='white')
canvas.pack()

# button that displays the plot
plot_button = Button(master=window, command=plot, height=2, width=10, text="Run Plot")

clear_button = Button(master=window, command=clear_plot, height=2, width=10, text="clear", background="yellow")

# place the button
plot_button.pack()
clear_button.pack()

# run the gui
window.mainloop()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,我可以为您提供一个示例代码,演示如何使用tkinter库,结合matplotlib,实现输入函数表达式后,绘制函数像的功能。代码如下: ```python import numpy as np import matplotlib.pyplot as plt from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg import tkinter as tk # 定义绘函数 def plot_graph(expr): # 定义x轴数据 x = np.linspace(-10, 10, 1000) # 将表达式的x替换为数组,将表达式转换为可计算的函数 f = eval("lambda x: " + expr) # 计算y轴数据 y = f(x) # 绘制函数像 fig = plt.figure(figsize=(5, 4), dpi=100) plt.plot(x, y) # 将绘制的像添加到tkinter窗口 canvas = FigureCanvasTkAgg(fig, master=root) canvas.draw() canvas.get_tk_widget().pack() # 定义tkinter窗口 root = tk.Tk() root.title("绘制函数像") # 定义函数表达式的输入框 expr_entry = tk.Entry(root, width=40) expr_entry.pack() # 定义绘按钮,点击时调用plot_graph函数进行绘 plot_btn = tk.Button(root, text="绘", command=lambda: plot_graph(expr_entry.get())) plot_btn.pack() # 启动tkinter窗口 root.mainloop() ``` 运行这段代码后,程序会弹出一个tkinter窗口,包含一个输入框和一个按钮。您可以在输入框输入函数表达式,例如:`x**2+2*x+1`,然后点击绘按钮,程序会根据您输入的表达式,绘制出对应函数的像。需要注意的是,这段代码同样没有对用户输入的表达式进行任何安全性检查,因此需要谨慎使用。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ko-walski

太难了,求鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值