240810-Gradio自界说Button按钮+事件函数+按钮图标样式设定 ...

打印 上一主题 下一主题

主题 556|帖子 556|积分 1668

A. 最终效果


B. 参考代码

要通过自界说HTML按钮来触发Gradio自带按钮的 click 函数,你可以使用JavaScript来模拟点击Gradio的按钮。这里是一个示例代码,展示了如何实现这一点:
  1. import gradio as gr
  2. # 自定义的 JavaScript,用于捕捉按钮点击并触发 Gradio 按钮的 click 事件
  3. custom_js = """
  4. <script>
  5. function triggerButton() {
  6.     document.getElementById("gradio_button").click();
  7. }
  8. </script>
  9. """
  10. # Gradio 应用
  11. with gr.Blocks(head=custom_js) as demo:
  12.     # Gradio 按钮
  13.     action_button = gr.Button(value="Gradio Button", elem_id="gradio_button")
  14.    
  15.     # 自定义 HTML 按钮
  16.     custom_html = """
  17.     <button οnclick="triggerButton()">Custom HTML Button</button>
  18.     """
  19.    
  20.     # 显示自定义 HTML 按钮
  21.     gr.HTML(custom_html)
  22.    
  23.     # 文本框用于显示点击结果
  24.     textbox = gr.Textbox()
  25.    
  26.     # 绑定 Gradio 按钮点击事件
  27.     action_button.click(lambda: "Gradio Button Clicked!", None, textbox)
  28.    
  29. demo.launch()
复制代码
C. 代码阐明


  • custom_js: 这是自界说的JavaScript代码,用于界说 triggerButton 函数。当点击自界说的HTML按钮时,triggerButton 函数会模拟点击Gradio的按钮。
  • Gradio按钮: 使用 gr.Button 创建Gradio按钮,并为其分配 elem_id 为 "gradio_button"。
  • 自界说HTML按钮: 使用 gr.HTML 插入自界说的HTML按钮,并设置点击时调用 triggerButton 函数。
  • 事件绑定: 使用 action_button.click 方法绑定按钮的点击事件,将效果输出到文本框。
运行这段代码后,你会看到一个Gradio生成的按钮和一个自界说的HTML按钮。当点击自界说HTML按钮时,Gradio按钮的 click 事件会被触发,文本框会显示相应的效果。
D. 自界说代码

  1. import gradio as gr
  2. custom_css = """
  3. body, html {
  4.     height: 100%;
  5.     margin: 0;
  6.     display: flex;
  7.     justify-content: center;
  8.     align-items: center;
  9.     padding: 10px;
  10.     box-sizing: border-box;
  11. }
  12. .button-container {
  13.     width: 100%;
  14.     max-width: 400px; /* 可调整最大宽度 */
  15. }
  16. .centered-button {
  17.     width: 100%;
  18.     border: 2px solid gray;
  19.     border-radius: 12px;
  20.     padding: 10px;
  21.     background-color: white;
  22.     cursor: pointer;
  23.     display: flex;
  24.     justify-content: center;
  25.     align-items: center;
  26.     box-sizing: border-box;
  27. }
  28. .centered-button svg {
  29.     width: 24px;
  30.     height: 24px;
  31. }
  32. """
  33. # 自定义的 JavaScript,用于捕捉按钮点击并触发 Gradio 按钮的 click 事件
  34. custom_js = """
  35. <script>
  36. function triggerButton() {
  37.     document.getElementById("gradio_button").click();
  38. }
  39. </script>
  40. """
  41. # Gradio 应用
  42. with gr.Blocks(head=custom_js, css=custom_css) as demo:
  43.     with gr.Row():
  44.         with gr.Column():
  45.             # 输入文本
  46.             input = gr.Textbox(label='Input')
  47.         with gr.Column():
  48.             # 输出文本
  49.             output = gr.Textbox(label='Output')
  50.     # 默认带icon的按钮
  51.     default_button1 = gr.Button(
  52.         icon='https://img.icons8.com/ink/48/linux.png', value='Web-Icon')
  53.     default_button2 = gr.Button(
  54.         icon='icons8-windows-ios-17-glyph-32.png', value='Local-Icon')
  55.     # 隐藏真实按钮
  56.     action_button = gr.Button(value="Gradio Button",
  57.                               elem_id="gradio_button", visible=False)
  58.     # 自定义 HTML 按钮
  59.     custom_html = """
  60.     <div class="button-container">
  61.     <button class="centered-button" οnclick="triggerButton()"><svg t="1723284361620" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6820" width="32" height="32"><path d="M747.4 535.7c-0.4-68.2 30.5-119.6 92.9-157.5-34.9-50-87.7-77.5-157.3-82.8-65.9-5.2-138 38.4-164.4 38.4-27.9 0-91.7-36.6-141.9-36.6C273.1 298.8 163 379.8 163 544.6c0 48.7 8.9 99 26.7 150.8 23.8 68.2 109.6 235.3 199.1 232.6 46.8-1.1 79.9-33.2 140.8-33.2 59.1 0 89.7 33.2 141.9 33.2 90.3-1.3 167.9-153.2 190.5-221.6-121.1-57.1-114.6-167.2-114.6-170.7z" p-id="6821"></path><path d="M642.3 230.7c50.7-60.2 46.1-115 44.6-134.7-44.8 2.6-96.6 30.5-126.1 64.8-32.5 36.8-51.6 82.3-47.5 133.6 48.4 3.7 92.6-21.2 129-63.7z" p-id="6822"></path></svg> SVG-Icon
  62.     </button>
  63.     </div>
  64.     """
  65.     # 显示自定义按钮
  66.     gr.HTML(custom_html)
  67.     # 绑定 Gradio 按钮点击事件
  68.     action_button.click(lambda x: f"Hello, {x}.", input, output)
  69. demo.launch(inbrowser=True)
复制代码
E. 样式阐明

要让按钮中的矢量图居中,你可以使用 display: flex 和 align-items: center、justify-content: center 来实现水平和垂直居中对齐。以下是一个包含矢量图(例如一个简单的SVG图标)的按钮,图标将居中显示:
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Centered Button with SVG</title>
  7.     <style>
  8.         body, html {
  9.             height: 100%;
  10.             margin: 0;
  11.             display: flex;
  12.             justify-content: center;
  13.             align-items: center;
  14.             padding: 10px;
  15.             box-sizing: border-box;
  16.         }
  17.         .button-container {
  18.             width: 100%;
  19.             max-width: 400px; /* 可调整最大宽度 */
  20.         }
  21.         .centered-button {
  22.             width: 100%;
  23.             border: 2px solid gray;
  24.             border-radius: 12px;
  25.             padding: 10px;
  26.             background-color: white;
  27.             cursor: pointer;
  28.             display: flex;
  29.             justify-content: center;
  30.             align-items: center;
  31.             box-sizing: border-box;
  32.         }
  33.         .centered-button svg {
  34.             width: 24px;
  35.             height: 24px;
  36.         }
  37.     </style>
  38. </head>
  39. <body>
  40. <div class="button-container">
  41.     <button class="centered-button">
  42.         <svg width="24px" height="24px" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
  43.             <g transform-origin="center" transform="scale(1)">
  44.                 <path d="M12 0C6.486 0 2 4.486 2 10C2 15.514 6.486 20 12 20C17.514 20 22 15.514 22 10C22 4.486 17.514 0 12 0zM12 18C7.589 18 4 14.411 4 10C4 5.589 7.589 2 12 2C16.411 2 20 5.589 20 10C20 14.411 16.411 18 12 18zM12 5C10.346 5 9 6.346 9 8C9 9.654 10.346 11 12 11C13.654 11 15 9.654 15 8C15 6.346 13.654 5 12 5zM12 13C10.346 13 9 14.346 9 16C9 17.654 10.346 19 12 19C13.654 19 15 17.654 15 16C15 14.346 13.654 13 12 13z" fill="currentColor"/>
  45.             </g>
  46.         </svg>
  47.     </button>
  48. </div>
  49. </body>
  50. </html>
复制代码
代码解释:


  • display: flex; justify-content: center; align-items: center;:

    • 这行代码在 .centered-button 中使用 flexbox 结构,将按钮中的内容(即 SVG 图标)水平和垂直居中。

  • svg 标签:

    • 这是一个简单的圆形图标示例。你可以替换为任何你想要的矢量图标。
    • width="24px" 和 height="24px":设置SVG图标的宽高为24px。
    • fill="currentColor":SVG的填充颜色将继承按钮的文本颜色,如许当你改变按钮的颜色时,SVG图标颜色也会随之变化。

  • .centered-button svg 样式:

    • 控制SVG图标的大小,使其适合按钮,并确保其在按钮中居中。

运行效果:

按钮会在页面中居中显示,按钮中的矢量图标也会在按钮内部居中显示,并且图标的大小可以根据必要进行调解。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

大号在练葵花宝典

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表