首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tkinter和Matplotlib中的按钮

Tkinter和Matplotlib中的按钮
EN

Stack Overflow用户
提问于 2020-12-21 06:11:41
回答 1查看 70关注 0票数 0

我想通过按下按钮在Tkinter中生成Matplotlib图。该按钮应启动数据采集例程(最终从串行端口),并在采集过程完成时绘制数据。下面的代码经过简化,只包含一些虚拟数据。我的问题是,当按下"plot“按钮时,没有出现图形。

代码语言:javascript
复制
import tkinter
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg)
from matplotlib.figure import Figure

window = tkinter.Tk()

fig = Figure(figsize=(5, 4), dpi=100)
ax = fig.add_subplot(1, 1, 1)
canvas = FigureCanvasTkAgg(fig, master=window) 
canvas.draw()

def plotData(ax):
    xs = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    ys = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    ax.clear()
    ax.plot(xs, ys)
   
CloseButton = tkinter.Button(master=window, text="Close", command=window.destroy)
PlotButton = tkinter.Button(master=window, text = "Plot", command = lambda: plotData(ax))
    
CloseButton.pack(side=tkinter.LEFT, padx = 10, pady = 10)
PlotButton.pack(side=tkinter.LEFT, padx = 10, pady = 10)

canvas.get_tk_widget().pack(side=tkinter.RIGHT, fill=tkinter.BOTH, expand=1)

tkinter.mainloop()
EN

回答 1

Stack Overflow用户

发布于 2020-12-21 06:23:23

您需要在函数的末尾添加canvas.draw()

代码语言:javascript
复制
def plotData(ax):
    xs = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    ys = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    ax.clear()
    ax.plot(xs, ys)
    canvas.draw()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65385298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档