How can I change the colour of a button when clicked at runtime? [duplicate]

眉间皱痕 提交于 2021-01-27 18:43:29

问题


button1=Button(root,text="A1",width=8).grid(row=0,column=0)
button2=Button(root,text="A2",width=8).grid(row=0,column=1)

label1=Label(root,text="       ",padx=20).grid(row=0,column=2)

button22=Button(root,text="A3",width=8).grid(row=0,column=3,sticky='E')
button23=Button(root,text="A4",width=8).grid(row=0,column=4,sticky='E')

I'm trying to make seat arrangement system for a school project. I have an issue: how can I change the colour of the button once it is clicked? I want to change the colour of the booked and available seats once I click on that button.


回答1:


If you just wish to change the color of a button when clicked, you need to use the .config() method on that button widget.

for example, if a button is defined like

aButton = tk.Button(root, text='change my color').pack()

then to change the color (or pretty much everything related to that widget like text, command or whatever) call the method

aButton.configure(bg='#f0f', fg='#fff') # change to your required colors, bg is background, fg is foreground.

OR .config() can be also be used (these 2 methods do exactly the same)

aButton.config(bg='#f0f', fg='#fff')

Now how will you know when a button is clicked or not. The simplest and intuitive way is to define functions and connect (or bind) them to buttons. Now how you wanna do that is totally up to the user's preferences. Some prefer to create separate different functions for all buttons, some only like to create one.

For your case though, as you don't need to do anything additional than changing colors then single function is enough. Important In the example code below, I have used lambda functions, a special type of functions, which don't need to be defined separately. That however, by no means, is necessary

Working example for you

from tkinter import *  # I don't recommend using global import. better use "import tkinter as tk"

root = Tk()

button1=Button(root,text="A1",width=8, command=lambda: button1.config(bg='#f00'))
button1.grid(row=0,column=0)

button2=Button(root,text="A2",width=8, command=lambda: button2.config(bg='#f00'))
button2.grid(row=0,column=1)

Label(root,text=" ",padx=20).grid(row=0,column=2)

button22=Button(root,text="A3",width=8, command=lambda: button22.config(bg='#f00'))
button22.grid(row=0,column=3,sticky='E')

button23=Button(root,text="A4",width=8, command=lambda: button23.config(bg='#f00'))
button23.grid(row=0,column=4,sticky='E')

root.mainloop()

Using functions

from tkinter import *  # I don't recommend using global import. better use "import tkinter as tk"


def changeColor(btn):
    # Use your own highlight background argument here instead of bg
    btn.configure(bg='#f00')


root = Tk()

button1=Button(root,text="A1",width=8, command=lambda: changeColor(button1))
button1.grid(row=0,column=0)

button2=Button(root,text="A2",width=8, command=lambda: changeColor(button2))
button2.grid(row=0,column=1)

Label(root,text=" ",padx=20).grid(row=0,column=2)

button22=Button(root,text="A3",width=8, command=lambda: changeColor(button22))
button22.grid(row=0,column=3,sticky='E')

button23=Button(root,text="A4",width=8, command=lambda: changeColor(button23))
button23.grid(row=0,column=4,sticky='E')

root.mainloop()


来源:https://stackoverflow.com/questions/64207078/how-can-i-change-the-colour-of-a-button-when-clicked-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!