我已经用Tkinter编写了一个小的GUI程序,因为我喜欢将更新排队到mainloop以进行连续的项目更新,以便创建一个由数据更改引起的更改圆形颜色的交互式动画。
(这是我发现matplotib不能很好地使用的东西,即如果我使用补丁并且我想更新补丁的颜色我发现我必须在现有补丁之上添加新的补丁,留下颜色残留物)
但是,我想从Matplotlib的工具栏中受益,以便我可以使用缩放和平移功能。
我正在尝试使用一个小的测试程序来实现这一点,为了简单起见,并在同一个窗口中获得了matplotlib和tkinter ......但是缩放不起作用!
我有2周的Matplotlib经验和Tkinter的几天经验,所以请在任何不准确的陈述中纠正我或添加其他建议。
这是我编写的用于测试的小程序,其中matplotlib zoom不起作用:
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import sys
from Tkinter import *
import matplotlib.backends.backend_tkagg as tkagg
class App:
def __init__(self, master):
self.frame = Frame(master, bg='#FFFFFF', height=1000, width=1000,
borderwidth=1, relief=RAISED)
self.frame.pack(expand=YES, fill=BOTH)
self.button = Button(
self.frame, text="QUIT", fg="red", command=self.frame.quit
)
self.button.pack(side=BOTTOM)
self.hi_there = Button(self.frame, text="Hello", command=self.say_hi)
self.hi_there.pack(side=BOTTOM)
###
# Creating a canvas for Tk items
self.tkcanvas = Canvas(self.frame, height=600, width=600)
self.tkcanvas.config(background='yellow')
self.tkcanvas.pack()
self.tkcanvas.create_rectangle(0,0,100,100,fill='blue')
# Hoping to utilize the mpl toolbar for zooming function by doing:
self.fig = Figure(figsize=(4,.5))
self.mplcanvas = FigureCanvasTkAgg(self.fig, master=self.frame)
self.mplcanvas.get_tk_widget().pack(side='bottom', fill='both')
self.mplcanvas._tkcanvas.pack(side='bottom', fill='both', expand=1)
#
self.mpltoolbar = NavigationToolbar2TkAgg( self.mplcanvas, master )
self.mpltoolbar.update()
self.mpltoolbar.pack()
###
def say_hi(self):
print "hi there, everyone!"
root = Tk()
app = App(root)
root.mainloop()
root.destroy()