Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

python - How to change the colour of a button whilst it is being pressed?

I've designed a GUI for a text based adventure game. I need to do some final tweaks to make sure it works smoothly.

I have several buttons (New Game, Exit Button etc.) which are represented by images. I've set the background colour of those button to #073a64 so they would match the colour of the top bar. Image of Top Bar

However, when the button is pressed, it turn white / grey for the duration of the press. Is it possible to set / change the colour of the button whilst it is being pressed?

from tkinter import *
trialGUI = Tk()
trialGUI.geometry('710x320')
trialGUI.title("Test GUI")
trialGUI.configure(background='#073a64')
trialGUI.columnconfigure(0, weight=1)
trialGUI.rowconfigure(0, weight=1)


trialButton = Button(trialGUI, bg='#073a64', height = 10, width = 20)
trialButton.grid()

trialGUI.mainloop()
question from:https://stackoverflow.com/questions/65902956/how-to-change-the-colour-of-a-button-whilst-it-is-being-pressed

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use:

activebackground="color"

Hence it should be:

trialButton = Button(trialGUI, bg='#073a64', height = 10, width = 20, activebackground="red")

This will color the button in red whilst is pressed


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...