You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在IntSlider数值后添加字符串并增大变量描述字号?

解决方案

直接看修改后的完整代码,关键改动已标注:

%matplotlib inline
from ipywidgets import widgets, interactive  # 补充导入widgets模块
import matplotlib.pyplot as plt
import numpy as np

# 增大描述字号+添加计量单位
a = widgets.IntSlider(
    description='Força', 
    value=500, 
    min=1, 
    max=1000, 
    step=1,
    style={'description_font_size': '16px'},  # 设置描述字号
    readout_format='{} N'  # 添加单位N
)
b = widgets.IntSlider(
    description='Comprimento', 
    value=100, 
    min=1, 
    max=100, 
    step=1,
    style={'description_font_size': '16px'},
    readout_format='{} cm'  # 添加单位cm
)
c = widgets.IntSlider(
    description='Módulo de Elasticidade', 
    value=200, 
    min=1, 
    max=1000, 
    step=1,
    style={'description_font_size': '16px'},
    readout_format='{} GPa'  # 添加单位GPa
)
d = widgets.IntSlider(
    description='Altura', 
    value=100, 
    min=1, 
    max=100, 
    step=1,
    style={'description_font_size': '16px'},
    readout_format='{} mm'  # 添加单位mm
)
e = widgets.IntSlider(
    description='Espessura', 
    value=100, 
    min=1, 
    max=100, 
    step=1,
    style={'description_font_size': '16px'},
    readout_format='{} mm'  # 添加单位mm
)

def delta(Forca, Comprimento, E, Altura, Espessura): 
    Area = Espessura*Altura
    deslocamento = (Comprimento*Forca*1e2)/(E*Area)
    print(f'\nδ = {(deslocamento/1e6):.4}mm\n')
    x1 = np.array([0, 0, Comprimento, Comprimento, 0])
    y1 = np.array([-Altura/2, Altura/2, Altura/2, -Altura/2, -Altura/2])
    x2 = np.array([Comprimento, Comprimento, Comprimento+deslocamento, Comprimento+deslocamento, Comprimento])    
    fig, ax = plt.subplots()   
    x3 = Comprimento+deslocamento
    x_direcao = x3 + Forca
    ax.quiver(x3, 0, x_direcao, 0, color="red")
    plt.plot(120, 55, '-', -10, -55,'-',x1, y1, "k-",x2, y1,"r--" )
    plt.show()

out = widgets.interactive_output(delta, {'Forca': a, 'Comprimento': b, 'E': c, 'Altura' : d, 'Espessura' : e})
widgets.VBox([widgets.VBox([a, b, c, d, e]), out])

关键改动说明

  • 补充模块导入:原代码仅导入了interactive,缺少widgets模块的导入,导致无法调用widgets.IntSlider,已修正。
  • 增大描述字号:通过style={'description_font_size': '16px'}参数调整描述文本的字号,可根据需求修改16px为其他数值(如18px)。
  • 添加计量单位:使用readout_format参数自定义数值显示格式,在数值后拼接对应物理量的单位(如力的单位N、长度单位cm等),单位可根据实际物理意义调整。

内容的提问来源于stack exchange,提问作者Emilly Raiane

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 07:35:22