python - 在 tkinter 上清除和绘制 mathplot 图形
问题描述
我需要一些关于我当前代码的帮助。我想通过 tkinter 创建一个窗口,并在我之前由 matplotlib 创建的画布中显示一个绘图。这一点我还没有达到。我的问题是我想通过点击一个按钮来清除这个画布。为了清除画布,我想先初始化它,然后才能用绘图填充它。
所以我的问题是:如何在创建的画布中填充绘图?
您可以在下面找到一个显示我的艺术状态的小代码。
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() ```
解决方案
没有直接的方法可以从数学绘图画布中清除图形。因此,您可以通过使用 tkinter canvas 的方法销毁小部件本身来清除destroy
画布(请注意,您不能销毁 mathplot 画布本身,因为它没有任何方法,例如销毁)。
要将数学绘图画布放在 tkinter 画布上,只需将 master 设置为canvas
object ( 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')
也可以
推荐阅读
- composer-php - 为什么推荐的带有 Composer 的 Drupal 安装方法使用 dev 作为稳定性标志?
- php - 在 null zendframework 3 上调用成员函数 hello()
- vue.js - Vue - 单个文件组件中的翻译
- ios - 我可以控制 concurrentPerform 的并行性吗?
- javascript - javascript 从字符串中提取主题标签
- ghost-blog - 幽灵到媒体的整合
- java - 如何在 Javafx 中删除视频开头的主窗口闪烁?
- python - 在键级python下面的字典中添加新元素
- xslt - xslt 多个插入节点(如果不存在)
- c# - Swashbuckle 在 asp.net 核心中失败,出现 NotSupportedException 异常