Python 用Tkinter处理绘图

Python 用Tkinter处理绘图,python,matplotlib,tkinter,Python,Matplotlib,Tkinter,我想用一些Tkinter按钮来处理绘图窗口。例如,使用按钮打印矩阵列和切换列。我试过这个: import numpy import pylab import Tkinter pylab.ion() # Functions definitions: x = numpy.arange(0.0,3.0,0.01) y = numpy.sin(2*numpy.pi*x) Y = numpy.vstack((y,y/2,y/3,y/4)) #Usual plot depending on a para

我想用一些Tkinter按钮来处理绘图窗口。例如,使用按钮打印矩阵列和切换列。我试过这个:

import numpy
import pylab
import Tkinter

pylab.ion()
# Functions definitions:
x = numpy.arange(0.0,3.0,0.01)
y = numpy.sin(2*numpy.pi*x)
Y = numpy.vstack((y,y/2,y/3,y/4))

#Usual plot depending on a parameter n:
def graphic_plot(n):
    if n < 0: n = 0
    if n > len(Y): n = len(Y)-1
    fig = pylab.figure(figsize=(8,5))
    ax = fig.add_subplot(111)
    ax.plot(x,Y[n,:],'x',markersize=2)
    ax.set_xlabel('x title')
    ax.set_ylabel('y title')
    ax.set_xlim(0.0,3.0)
    ax.set_ylim(-1.0,1.0)
    ax.grid(True)
    pylab.show()


def increase(n):
   return n+1

def decrease(n):
    return n-1

n=0
master = Tkinter.Tk()
left_button  = Tkinter.Button(master,text="<",command=decrease(n))
left_button.pack(side="left")
right_button = Tkinter.Button(master,text=">",command=increase(n))
right_button.pack(side="left")
master.mainloop()
导入numpy
进口派拉布
进口Tkinter
pylab.ion()
#功能定义:
x=numpy.arange(0.0,3.0,0.01)
y=numpy.sin(2*numpy.pi*x)
Y=numpy.vstack((Y,Y/2,Y/3,Y/4))
#取决于参数n的常规绘图:
def图形_图(n):
如果n<0:n=0
如果n>len(Y):n=len(Y)-1
fig=pylab.图(figsize=(8,5))
ax=图添加_子批次(111)
坐标图(x,Y[n,:],'x',markersize=2)
ax.set_xlabel('x title')
ax.set_ylabel('y title')
ax.set_xlim(0.0,3.0)
ax.set_ylim(-1.0,1.0)
ax.grid(真)
pylab.show()
def增加(n):
返回n+1
def下降(n):
返回n-1
n=0
master=Tkinter.Tk()
左按钮=Tkinter.按钮(主按钮,文本=”,命令=增加(n))
右按钮包装(侧=“左”)
master.mainloop()
但是我不知道什么时候调用graphic_plot函数并根据
n
参数刷新图形。

首先,您需要在按钮中调用
命令
参数。在这部法典中

left_button  = Tkinter.Button(master, text="<", command=decrease(n))
对于按钮,我们将有:

model = SimpleModel()  # create a model

left_button  = Tkinter.Button(master, text="<", command=model.decrease)

right_button = Tkinter.Button(master, text=">", command=model.increase)
model=SimpleModel()#创建一个模型
左按钮=Tkinter.button(master,text=”“,command=model.increase)
model = SimpleModel()  # create a model

left_button  = Tkinter.Button(master, text="<", command=model.decrease)

right_button = Tkinter.Button(master, text=">", command=model.increase)