Python / Tikinter:无法使用按钮更新图表?

时间:2010-11-05 14:01:38

标签: python button tkinter

简化问题:出于某种原因,当我点击生成图按钮时,图表没有更新。

global f

master = Tk()

plotFrame = Frame(master)
plotFrame.pack(side=TOP)

f = Figure()

canvas = FigureCanvasTkAgg(f, plotFrame)
canvas.get_tk_widget().pack()
toolbar = NavigationToolbar2TkAgg(canvas,master)
toolbar.pack(side=BOTTOM)

def grph():
    graph1 = f.add_subplot(111)
    graph1.plot([1,2,3])

genButton = Button(master, text="Generate plots...", command=grph)
genButton.pack(side=LEFT,anchor=W)

master.mainloop()

1 个答案:

答案 0 :(得分:1)

添加以下行:

def grph():
    graph1 = f.add_subplot(111)
    graph1.plot([1,2,3])
    canvas.draw()