问题描述
我写了一个Python程序,我想在画布上多次绘制和擦除图形。我的Python代码如下所示。
问题是我只能一次绘制图形!单击绘图按钮后,单击清除按钮。然后,单击“绘图”按钮。但是,没有数字 不再!
是什么原因?有什么主意吗?
from tkinter import *
from matplotlib.figure import figure
from matplotlib.backends.backend_tkagg import (figureCanvasTkAgg,NavigationToolbar2Tk)
def clearPlot():
canvas.get_tk_widget().delete("all")
#canvas.delete("all")
return None
def plot(frame):
# the figure that will contain the plot
fig = figure(figsize = (5,5),dpi = 100)
print("inside the plot")
# list of squares
y = [i**2 for i in range(101)]
# adding the subplot
ax= fig.add_subplot(111)
# plotting the graph
ax.plot(y)
# creating the Tkinter canvas
# containing the Matplotlib figure
global canvas
canvas = figureCanvasTkAgg(fig,master = frame)
canvas.draw()
# placing the canvas on the Tkinter window
canvas.get_tk_widget().pack()
# creating the Matplotlib toolbar
toolbar = NavigationToolbar2Tk(canvas,frame)
toolbar.update()
# placing the toolbar on the Tkinter window
canvas.get_tk_widget().pack()
print("Exiting Plot")
return None
# the main Tkinter window
window = Tk()
# setting the title
window.title('Plotting in Tkinter')
# dimensions of the main window
window.geometry("800x700")
# Frame for TreeView
frame1 = LabelFrame(window,text="Data_Visualization")
frame1.place(height = 400,width = 500,rely = 0.3,relx = 0.2)
# Frame for Open File Dialog
frame2 = LabelFrame(window,text="Command Functions")
frame2.place(height = 150,rely = 0.0,relx = 0.2)
# button that displays the plot
plot_button = Button(master = frame2,command = lambda:plot(frame1),height = 2,width = 10,text = "Plot")
# place the button
# in main window
#plot_button.pack()
plot_button.place(rely = 0.0,relx = 0.2)
clear_button = Button(master = frame2,command = lambda:clearPlot(),text = "Clear")
# place the button
# in main window
#lear_button.pack()
clear_button.place(rely = 0.0,relx = 0.6)
# run the gui
window.mainloop()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)