python - 如何在 Python 中单击按钮时更改按钮颜色
问题描述
我正在使用 tKinter 模块在 Python 中制作带有按钮的 GUI。我有一个与背景融为一体的按钮,因此当您单击它时,您似乎正在选择文本(而不是按钮)。问题是,当您单击它时,该框会暂时变白,看起来非常难看。我想知道如何在您单击按钮时更改按钮的颜色。
我已经尝试查找此内容但无济于事。
这是我的意思的一个简单示例:
import tkinter as tk
def on_enter(e):
button['fg'] = 'yellow'
def on_leave(e):
button['fg'] = 'green'
win = tk.Tk()
win.title("Example")
win.geometry("600x400")
win.state('zoomed')
win.configure(background='blue')
button = tk.Button(win, height=3, width=10, borderwidth=0, text='Example', font=("Helvetica", 40), bg="blue", fg="grey",)
button.pack()
button.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
button.bind("<Enter>", on_enter)
button.bind("<Leave>", on_leave)
win.mainloop()
解决方案
使用参数activebackground
:
button = tk.Button(..., activebackground = "yellow")
当它被按下时,它会改变按钮的颜色。同样,您可以使用activeforeground
更改被按下按钮的文本颜色
推荐阅读
- php - 如何通过php将文件作为pdf文件的附件嵌入?
- android - 按下电源按钮后,Android 12 SplashScreen API 显示黑色
- javascript - 滚动时平滑“变换:翻译”
- python - 人脸识别项目中的dlib和人脸识别文件安装问题
- python - Python - 为什么“Pathlib.Path.touch()”附加一个“?” 到文件名
- postgresql - postgres,使用来自 jsonb 的数组生成列
- android - 通过 Retrofit2 POST 请求在 JSONObject 中获取 JSONObject
- javascript - 合并多个搜索结果 Angular 10
- git - Github 默认分支
- qt5 - 将单元测试添加到 QMake 项目而不将源移动到子目录