如何更改Shiny for Python中操作按钮的背景颜色?
修改Shiny for Python按钮背景颜色的方法
你可以通过两种常用方式修改按钮背景色,以下是具体实现:
方法一:内联CSS直接设置
直接在ui.input_action_button中通过style参数指定样式,适合快速给单个按钮设置样式:
from shiny.express import ui with ui.layout_columns(): ui.input_action_button( "red_button", "Make this button red!", style="background-color: #ff4444; border-color: #cc0000; color: white;" ) ui.input_action_button( "blue_button", "Make this button blue!", style="background-color: #33b5e5; border-color: #0099cc; color: white;" )
这里同时设置了背景色、边框色和文字色,让按钮样式更协调。
方法二:自定义CSS类(推荐)
先定义全局CSS类,再给按钮添加对应的类名,适合多个按钮复用相同样式,代码更整洁:
from shiny.express import ui # 定义自定义CSS样式 ui.tags.style(""" .btn-red { background-color: #ff4444 !important; border-color: #cc0000 !important; color: white !important; } .btn-blue { background-color: #33b5e5 !important; border-color: #0099cc !important; color: white !important; } """) with ui.layout_columns(): ui.input_action_button("red_button", "Make this button red!", class_="btn-red") ui.input_action_button("blue_button", "Make this button blue!", class_="btn-blue")
- 使用
class_参数(而非class,避免与Python关键字冲突)绑定自定义类 - 添加
!important是为了覆盖Shiny默认的按钮样式,确保自定义样式生效
内容的提问来源于stack exchange,提问作者Redz
相关产品推荐
相关产品推荐

