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

如何阻止PySimpleGUI按钮点击时的颜色变化及文本动画效果?

解决PySimpleGUI按钮点击时的颜色变化与文本动画问题

要实现点击后外观无变化的Material Design风格按钮,只需给sg.Button添加两个关键参数,按需可加第三个:

  • 设置press_colors与原button_color完全一致,阻止点击时的颜色切换
  • 设置disable_click_animation=True,取消文本的点击位移动画
  • (可选)设置mouseover_colors与原按钮颜色一致,移除鼠标悬停时的颜色变化

修改后的完整代码:

import PySimpleGUI as sg

variableText = "topline"
variableNum = 12345
layout = [
    [sg.Button(
        '\n'.join([variableText, str(variableNum)]),
        size=(8,2),
        button_color=("#FFFFFF","#9945B5"),
        font=('Arial', 8),
        border_width=0,
        bind_return_key=True,
        key='-BUTTON-',
        press_colors=("#FFFFFF","#9945B5"),  # 点击时颜色与常态保持一致
        disable_click_animation=True,  # 禁用文本点击时的位移动画
        mouseover_colors=("#FFFFFF","#9945B5")  # 可选:移除鼠标悬停的颜色变化
    )],
]

window = sg.Window("Button Color Example", layout, finalize=True)

while True:
    event, values = window.read()
    if event in (sg.WIN_CLOSED, 'Exit'):
        break

window.close()

参数说明:

  • press_colors:强制按钮按下状态的文本色、背景色与默认状态完全相同,消除点击时的颜色波动
  • disable_click_animation:直接关闭PySimpleGUI默认的按钮文本点击位移效果
  • mouseover_colors:如果需要完全无状态变化的按钮,添加此参数可移除鼠标悬浮时的颜色反馈

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 08:55:10