2016-04-11 14 views
0

2つのパラメータkaとkdの数値を入力し、ボタンをクリックしてシミュレーションを開始したいと考えています。次に、両方のパラメータを変更し、ボタンをクリックして別のシミュレーションを開始します。これで、BoundedFloatTextウィジェットで指定されたデフォルト値だけがメインプログラムに転送されたようです。 ありがとうございます。IPythonウィジェットBoundedFloatTextを使用して数値を入力する方法は?

%matplotlib inline 
import numpy as np 
from scipy.integrate import ode 
import matplotlib.pyplot as plt 
from ipywidgets import * 
from IPython.display import clear_output, display, HTML 
# 
def fmain (ka, kd): 
# 
    num_steps = 10 
# 
# Prepare plots 
    fig, ax = plt.subplots() 
#  
    τ = np.zeros((num_steps, 1)) 
    F = np.zeros(num_steps) 
    τ[0] = 0 
    F[0] = 0.0 
# 
# Integrate the system of ODEs across each delta_t timestep 
    kk = 1 
    while kk < num_steps:    
     τ[kk] = τ[kk-1] + 0.01  
     F[kk] = ka + kd*τ[kk] 
#     
     clear_output(wait=True) 
     ax.cla() 
     ax.plot(τ[0:kk], F[0:kk], color="blue", linestyle="solid", linewidth=2) 
     ax.set_ylabel(r'Dimensionless exit flow rate, $\bar F_A$ [-]') 
     ax.set_xlabel('Dimensionless time, $τ$ [-]') 
     ax.set_title('Numerical Simulation ka= %s, kd = %s'%(ka, kd)) 
     display(fig) 
#   
# end of time cycle  
     kk += 1   
#  
plt.close() 
# Specify widgets 

form = widgets.VBox() 
ka_in = BoundedFloatText(description="$$k_a:$$", 
          value=5.0, min=0.0, max=30.0, 
          padding = 4) 
kd_in = BoundedFloatText(description="$$k_d:$$", 
          value=5.0, min=0.0, max=30.0, 
          padding = 4) 
button = widgets.Button(description="Click to Run Program", 
        color="red",background_color= "lightgray") 
form.children = [ka_in, kd_in, button ] 
display(form) 
ka = ka_in.value 
kd = kd_in.value 
h4 = widgets.HTML("<br><i> Simulation started, be patient!</i><br>") 
def on_button_clicked(b): 
    display(h4) 
    fmain(ka , kd) 

button.on_click(on_button_clicked) 

答えて

0

私は私の質問で答えを見つけたと思います。

%matplotlib inline 
import numpy as np 
from scipy.integrate import ode 
import matplotlib.pyplot as plt 
from ipywidgets import * 
from IPython.display import clear_output, display, HTML 
# 
def fmain (ka, kd): 
# 
    num_steps = 10 
# 
# Prepare plots 
    fig, ax = plt.subplots() 
#  
    τ = np.zeros((num_steps, 1)) 
    F = np.zeros(num_steps) 
    τ[0] = 0. 
    F[0] = ka + kd*τ[0] 
# 
# Integrate the system of ODEs across each delta_t timestep 
    kk = 1 
    while kk < num_steps:  
     τ[kk] = τ[kk-1] + 0.01  
     F[kk] = ka + kd*τ[kk] 
#     
     clear_output(wait=True) 
     ax.cla() 
     ax.plot(τ[0:kk], F[0:kk], color="blue", linestyle="solid",  linewidth=2) 
     ax.set_ylabel(r'Dimensionless exit flow rate, $\bar F_A$ [-]') 
     ax.set_xlabel('Dimensionless time, $τ$ [-]') 
     ax.set_title('Numerical Simulation ka= %s, kd = %s'%(ka, kd)) 
     display(fig) 
#   
# end of time cycle  
     kk += 1   
#  
plt.close() 
# Specify widgets 

form = widgets.VBox() 
ka = BoundedFloatText(description="$$k_a:$$", 
          value=5.0, min=0.0, max=30.0, 
          padding = 4) 
kd = BoundedFloatText(description="$$k_d:$$", 
          value=5.0, min=0.0, max=30.0, 
          padding = 4) 
button = widgets.Button(description="Click to Run Program", 
        color="red",background_color= "lightgray") 
form.children = [ka, kd, button] 
display(form) 

h4 = widgets.HTML("<br><i> Simulation started, be patient!</i><br>") 
def on_button_clicked(b): 
    display(h4) 
    fmain(ka.value, kd.value) 
    h4.close() 
button.on_click(on_button_clicked) 
関連する問題