2016-04-11 20 views
0

İki parametre ka ve kd'nin sayısal değerlerini girmek ve simulasyonu başlatmak için bir butona tıklamak istiyorum. Ardından, her iki parametreyi de değiştirin, bir düğmeyi tıklayın ve başka bir simülasyon başlatın. Şimdi, BoundedFloatText widget'ında belirtilen varsayılan değerlerin ana programa aktarıldığı görülüyor. Teşekkür ederiz.IPython parçacığı BoundedFloatText kullanarak sayısal değerler nasıl girilir?

%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) 

cevap

0

Sanırım soruma bir cevap buldum.

%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) 
İlgili konular