Matplotlib有时在tkinter中更新

时间:2013-05-21 00:04:05

标签: matplotlib tkinter

我有一个带有GUI的程序,包括多个滑块和图形。滑块设置函数的参数,该函数应该绘制在图形上。我开始遵循http://matplotlib.org/examples/user_interfaces/embedding_in_tk.html中的指示,从包改为网格,并尝试了广告恶心。如果我移动一个滑块,我现在更新的情节,但如果我移动另一个滑块,则不会更新。我不认为两者之间存在差异。

import matplotlib
matplotlib.use('TkAgg')
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from Tkinter import *

domain_min = 1
domain_max = 10
order_min = 0
order_max = 3
fig = Figure(figsize=(10,5), dpi=50)

def module(x):
    global domain, order, output_message, fig, a, x_vals, y_vals

    domain = float(domain_slide.get())
    order = int(order_slide.get())

    output_message = 'f(1) = %i\nf(2) = %i\nf(3) = %i\nf(4) = %i\nf(5) = %i\nf(6) = %i\n\
f(7) = %i\nf(8) = %i\nf(9) = %i\nf(10)= %i'%(1**order,2**order,3**order,4**order,5**order,\
                                             6**order,7**order,8**order,9**order,10**order)

    output_message_text.set(output_message)

    x_vals = np.linspace(0,domain,100)
    y_vals = x_vals**order

    a = fig.add_subplot(111)
    a.clear()
    a.plot(x_vals,y_vals,color='blue')

#GUI
root = Tk()

domain_slide = DoubleVar()
order_slide = DoubleVar()
output_message_text = StringVar()

ds = Scale(root, variable = domain_slide, from_ = domain_min, to = domain_max, command = module)
ds.grid(column = 0, row = 0)

o_s = Scale(root, variable = order_slide, from_ = order_min, to = order_max, command = module)
o_s.grid(column = 1, row = 0)

out_message = Message(root, textvariable = output_message_text, width = 300, bg = 'white').grid(column = 0, row = 1, rowspan = 3)


canvas = FigureCanvasTkAgg(fig, master=root)
canvas.show()
canvas.get_tk_widget().grid(column=3,row=1)
canvas.show()

label = Label(root)
label.grid()

root.mainloop()

python在两个滑块中看到了什么区别?在程序的更深入版本中,没有一个滑块更新图表。

1 个答案:

答案 0 :(得分:0)

您需要添加

fig.cavas.draw()

到你的回调函数。

我不太清楚为什么永远重新绘制画布。