0

我有一个带有 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 在这两个滑块中看到了什么区别?在程序的更深入版本中,没有一个滑块更新绘图。

4

1 回答 1

0

您需要添加一个

fig.cavas.draw()

到你的回调函数。

我不太清楚为什么它会在没有这个的情况下重绘画布。

于 2013-05-21T01:27:10.737 回答