如何解决如何使Tkinter GUI只绘制一次Matplotlib图
我有一些Tkinter代码,可以编写4tabbed gui。在GUI tab2中有一个用于绘制简化图形的按钮,该按钮有效,但是当我再次单击该按钮时,它将在第一个图形的正下方重新打印画布和图形。我已经研究了如何使用“关闭”或“清除”按钮删除图,但确实需要它仅绘制一次(即使再次单击该按钮也是如此)。
代码
was_plotted = False
def plot():
global was_plotted
if(was_plotted) == False:
# the figure that will contain the plot
fig = Figure(figsize = (5,5),dpi = 100)
y = [i**2 for i in range(101)] # list of squares
# adding the subplot
plot1 = fig.add_subplot(111)
# plotting the graph
plot1.plot(y)
canvas = FigureCanvasTkAgg(fig,master = tab2) # creating the Tkinter canvas containing the Matplotlib figure
canvas.draw()
canvas.get_tk_widget().grid() # placing the canvas on the Tkinter window
was_plotted = True
所需的输出
再按一次Plot按钮不会导致图形出现在第一个图形下方。
来自xszym的想法
Entry.objects.filter(pub_date__year=2005).order_by('-pub_date','headline')
解决方法
您可以通过设置全局标记was_plotted
was_plotted = False
def plot():
global was_plotted
if(was_plotted):
return
was_plotted = True
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。