python - Plotly Dash:单击时如何更改按钮颜色?
问题描述
嗨,我在破折号中设计了 3 个按钮,我想在单击按钮时更改按钮的颜色,我知道 @app.callback 中有更改,但我不确定要更改什么。我想保持一切不变,但颜色应更改为“红色”
谁能帮我这个。
leftButtons = html.Div(id='framework-left-pane',
children=[
dcc.Link( children = html.Button(leftButton1,
id='leftbutton1',
className='roundbutton',
style={'backgroundColor': '#111100', 'color':'white','width':'100%', 'border':'1.5px black solid', 'height': '50px','text-align':'center', 'marginLeft': '20px', 'marginTop': 130}, n_clicks = 0)),
dcc.Link( children = html.Button(leftButton2,
id='leftbutton2',
className='roundbutton',
style={'backgroundColor': '#111100', 'color':'white','width':'100%' , 'border':'1.5px black solid','height': '50px','text-align':'center', 'marginLeft': '20px', 'marginTop': 20},n_clicks = 0)),
dcc.Link( children = html.Button(leftButton3,
id='leftbutton3',
className='roundbutton',
style={'backgroundColor': '#111100', 'color':'white','width':'100%', 'border':'1.5px black solid','height': '50px','text-align':'center', 'marginLeft': '20px', 'marginTop': 20},n_clicks = 0)),
],style={'width':'200px','position':'fixed'})
`
所以现在使用 app.callback 我只返回一个图像
@app.callback(
Output(component_id='tab_1_images', component_property='children'),
[Input('leftbutton1', 'n_clicks'),
Input('leftbutton2', 'n_clicks'),
Input('leftbutton3', 'n_clicks')])
def update_output_div(n_clicks_A, n_clicks_B, n_clicks_C):
return [a static image for each of the button]
解决方案
要在单击按钮后更改按钮的颜色,您需要style
在回调中更新相应的属性:
import dash
import dash_html_components as html
from dash.dependencies import Input, Output
app = dash.Dash(__name__)
white_button_style = {'background-color': 'white',
'color': 'black',
'height': '50px',
'width': '100px',
'margin-top': '50px',
'margin-left': '50px'}
red_button_style = {'background-color': 'red',
'color': 'white',
'height': '50px',
'width': '100px',
'margin-top': '50px',
'margin-left': '50px'}
app.layout = html.Div([
html.Button(id='button',
children=['click'],
n_clicks=0,
style=white_button_style
)
])
@app.callback(Output('button', 'style'), [Input('button', 'n_clicks')])
def change_button_style(n_clicks):
if n_clicks > 0:
return red_button_style
else:
return white_button_style
if __name__ == '__main__':
app.run_server(debug=True)
推荐阅读
- python - 为什么 add_child 方法在我使用 folium 库的地理映射代码中不起作用?
- html - 移动 Safari 菜单链接和页脚不显示字体系列
- c++ - 鼠标单击时的 C++ 屏幕截图不起作用
- xml - XSLT 2.0 围绕指定的子元素拆分元素
- java - 如何从具有相同电子邮件地址的房间数据库中检索数据列表
- cgal - 将 AABB_tree 与多个表面网格一起使用
- ios - 的值没有成员
- c# - 如何使用 Linq 添加列表数据到列表子类?
- .htaccess - .htaccess 从一个 pdf 重定向或重写到另一个
- google-chrome - 是否可以在 Travis Chrome 上使用 --disable-web-security ?