在 tkinter 上清除并绘制 matplotlib 图形

2024-03-15

我当前的代码需要一些帮助。我想通过 tkinter 创建一个窗口,并在我之前通过 matplotlib 创建的画布中显示一个绘图。这一点我还没有达到。我的问题是我想通过点击按钮来清除画布。为了清除画布,我想先初始化它,然后才能用绘图填充它。

So 我的问题 is: 如何在创建的画布中填充绘图?

下面你可以找到一段小代码,展示了我的艺术水平。

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

def plot(): 
    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 
    output = FigureCanvasTkAgg(fig, master = window)
    output.draw()

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

def clear_plot():
    canvas.delete('all') 

# the main Tkinter window 
window = Tk() 

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

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

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

# button that displays the plot 
plot_button = Button(master = window, command = plot, height = 2, width = 10, text = "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() ```

没有直接的方法可以从数学绘图画布中清除图形。因此,您可以通过使用销毁小部件本身来清除画布destroytkinter 画布的方法(请注意,您不能销毁 mathplot 画布本身,因为它没有任何方法,例如 destroy)。

要将数学绘图画布放在 tkinter 画布上,只需将 master 设置为canvas目的 (output = FigureCanvasTkAgg(fig, master = canvas))

(这是您更正后的代码)

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

def plot():
    global output, fig
    
    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 
    output = FigureCanvasTkAgg(fig, master = canvas)
    output.draw()

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

def clear_plot():
    global output
    if output:
        for child in canvas.winfo_children():
            child.destroy()
        # or just use canvas.winfo_children()[0].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 = "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()

或者你可以使用

def clear_plot():
    global output
    if output:
        output.get_tk_widget().destroy()
    output = None

这里的输出是你的FigureCanvasTkAgg对于其他希望实现这一目标的人来说。而你只是想暂时隐藏剧情output.get_tk_widget().pack_forget()并再次显示它output.get_tk_widget().pack()

update output.get_tk_widget()返回用于实现FigureCanvasTkAgg的Tk小部件,这意味着您还可以使用 帆布。所以,output.get_tk_widget().delete('all')也有效

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 tkinter 上清除并绘制 matplotlib 图形 的相关文章

随机推荐

  • 如何使类属性专属于超类

    我有一个关于行星的大师班 class Planet def init self name self name name def destroy self 我还有一些继承自的类Planet我想让其中之一无法被摧毁 而不是继承destroy功能
  • 使用动态 Linq 实体框架查询抛出奇怪的异常

    我有一个画廊实体框架类 我正在尝试使用 ScottGu 博客上发布的动态 Linq 库来查询实体集 失败的代码行如下 return context Galleries OrderBy sidx sord Skip page rows Tak
  • 二叉搜索树中节点的公平删除

    BST中删除节点的思路是 如果该节点没有子节点 则删除该节点并将父节点指向该节点的指针更新为空 如果该节点有一个子节点 则通过更新该节点的父节点指向其子节点的指针来用其子节点替换该节点 如果该节点有两个子节点 则找到该节点的前驱节点并将其替
  • SVG 中的动画虚线

    我需要为 SVG 中的虚线设置动画 My Work paths fill none stroke grey stroke dasharray 1440 stroke width 1440 stroke linejoin round mask
  • 从 Google 相册应用获取视频(非本地)

    随着谷歌照片应用程序 我正在尝试选择一个未缓存在设备上的视频 我正在使用ACTION 获取 内容意图 启动选项对话框 然后从那里我选择 Google Photos 应用程序 选择本地视频时 它会返回这种形式的 Uri 内容 媒体 外部 视频
  • 在 GSP 中导入和使用 Groovy 代码

    我正在尝试在 GSP 中使用常规函数 请帮忙 因为我正要在这里剃掉我的头发 在我的普惠制顶部我有 我的 GSP 里面有 p I have been in the heating and cooling business for p 和我的
  • ActiveAdmin:如何覆盖索引控制器操作:nil:NilClass 的未定义方法“base”

    我试图覆盖 ActiveAdmin 控制器的索引操作 以显示 current user 的结果而不是所有结果 controller do def index user tasks UserTask where user id gt curr
  • 在 C++ 中从 EnumWindows 生成的过滤/解析列表

    我正在使用以下代码来获取我的计算机上运行的窗口列表 include
  • 有用(困难)的 SQL 脚本库 [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 如何定义 MKAnnotationView 重叠的顺序?

    我的地图中有几个 MKAnnotations 及其相应的视图 有时会变得非常拥挤 现在 我的应用程序中的注释有两种风格 有些必然会保留在原处 而另一些则会随着时间的推移而移动 我更喜欢让视觉上更稳定的物体出现在背景中 而移动的物体总是在它们
  • 在 C# 中确定会话变量为 null 或空的最佳方法是什么?

    检查 ASP NET C 中会话变量是否存在的最佳方法是什么 我喜欢用String IsNullOrEmpty 适用于字符串 想知道是否有类似的方法Session 目前我知道的唯一方法是 var session if Session var
  • Finder 窗口刷新问题(Yosemite)

    我正在开发 Finder Sync 扩展以在文件和文件夹上应用徽章 我想刷新 Finder 应用程序中特定文件 文件夹的图标 有没有办法在 mac OS Yosemite 上以编程方式刷新查找器窗口 除了创建和删除文件 我也有这个问题 我曾
  • 用pointfree风格写f?

    说我有功能 g a gt b h a gt c and f b gt c gt d 函数可以写吗 f a gt a gt d 给出的 f x y f g x h y 点自由风格 可以写一个函数 f a gt d f x f g x h x
  • imul 指令的 ZF 行为是什么? [复制]

    这个问题在这里已经有答案了 指令集参考指出 imul 指令的 ZF 未定义 那么 如果我将寄存器中的值乘以立即数 0x0 ZF 会发生什么情况 未定义意味着结果可以是任何值 并且任何值都不重要
  • 使用 Tortoise SVN 通过 VPN 进行 SVN+SSH 结账,Smartsvn 失败

    系统和连接详细信息 我使用的是运行 Windows 7 的 64 位系统 我安装了 Open VPN 和 Tortoise SVN 64 位 该存储库位于远程系统中 我们使用 VPN 进行连接 到目前为止我做了什么 我已按照必要的步骤启动
  • Firestore 将值添加到数组字段

    我尝试使用 Firebase 云函数将聊天室的 id 添加到数组字段中的用户文档中 我似乎无法弄清楚写入数组字段类型的方法 这是我的云函数 exports updateMessages functions firestore documen
  • 仅返回 LEFT JOIN 的最新结果[重复]

    这个问题在这里已经有答案了 我正在查询两个表 students2014 和notes2014 中的数据 以便返回学生列表以及每个学生的注释 为此 我使用以下 select 语句 SELECT FROM students2014 LEFT J
  • Qt vtable错误

    我正在浏览旧的 Trolltech Qt 教程 因为我发现它们比新的教程更有帮助 并且undefined reference to vtable当我到达一个实现它自己的信号的类小部件时 我遇到了错误 http doc trolltech c
  • jquery 如何删除第一个 x div 的?

    如果我单击按钮 我会尝试删除前 4 个 div div class test div class 1 div div class 1 div div class 1 div div class 1 div div class 1 div d
  • 在 tkinter 上清除并绘制 matplotlib 图形

    我当前的代码需要一些帮助 我想通过 tkinter 创建一个窗口 并在我之前通过 matplotlib 创建的画布中显示一个绘图 这一点我还没有达到 我的问题是我想通过点击按钮来清除画布 为了清除画布 我想先初始化它 然后才能用绘图填充它