import numpy as np
from Tkinter import *
from ttk import *
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
def on_key_event(event):
print('you pressed %s'%event.key)
key_press_handler(event, canvas, toolbar)
def mOpen():
var = IntVar()
slider_1 = Scale(mGui,orient=HORIZONTAL,length = 100,from_=0,to=9,variable=var)
slider_1.place(x = 765,y=390)
t = np.arange(100000).reshape(100,100,10)
f = Figure(figsize=(5,4),dpi=100)
a = f.add_subplot(111)
a.imshow(t[:,:,var.get()])
print var.get()
canvas_3 = FigureCanvasTkAgg(f,master = mGui)
canvas_3.show()
canvas_3.get_tk_widget().place(x=5,y=5)
toolbar_3 = NavigationToolbar2TkAgg( canvas_3, mGui )
toolbar_3.update()
toolbar_3.place(x=10,y=15)
canvas_3._tkcanvas.place(x=7,y=7)
canvas_3.mpl_connect('key_press_event', on_key_event)
mGui = Tk()
mOpen()
mGui.geometry('900x900+300+10')
mGui.title('Plot')
mGui.mainloop()
我尝试使用a.imshow(t[:,:,var.get()])
命令更新绘图。但是,在更新比例时,第三个坐标(var.get()
)的值不会改变。
如何使用“缩放”窗口小部件输出更新绘图?
答案 0 :(得分:2)
每次修改窗口小部件时,都需要使用command=
参数Scale
来调用函数。在此功能中,您可以更新您的情节。
代码有点乱,因为你必须能够访问你的numpy数组以及该函数中的轴和画布,但是这样可行:
import numpy as np
from Tkinter import *
from ttk import *
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
def on_key_event(event):
print('you pressed %s'%event.key)
key_press_handler(event, canvas, toolbar)
def mOpen():
global t
global a
global canvas_3
var = IntVar()
slider_1 = Scale(mGui,orient=HORIZONTAL,length = 100,from_=0,to=9, variable=var, command=updateScale)
slider_1.place(x = 765,y=390)
t = np.random.random_sample(100000).reshape(100,100,10)
f = Figure(figsize=(5,4),dpi=100)
a = f.add_subplot(111)
a.imshow(t[:,:,var.get()])
print var.get()
canvas_3 = FigureCanvasTkAgg(f,master = mGui)
canvas_3.show()
canvas_3.get_tk_widget().place(x=5,y=5)
toolbar_3 = NavigationToolbar2TkAgg( canvas_3, mGui )
toolbar_3.update()
toolbar_3.place(x=10,y=15)
canvas_3._tkcanvas.place(x=7,y=7)
canvas_3.mpl_connect('key_press_event', on_key_event)
def updateScale(var):
print "scale is now %s" % (var)
a.imshow(t[:,:,int(float(var))])
canvas_3.show()
mGui = Tk()
mOpen()
mGui.geometry('900x900+300+10')
mGui.title('Plot')
mGui.mainloop()