3
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.

dspencer
  • 4,297
  • 4
  • 22
  • 43
shalini
  • 33
  • 3

1 Answers1

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()
P S Solanki
  • 1,033
  • 2
  • 11
  • 26
  • 1
    Thank you so much . I still have a question like how will I be able to see the colour change I tried using config but it doesn't change from the default one to red . how can I show that ? – shalini Oct 05 '20 at 12:30
  • @shalini In the example code above (below "working example for you" heading), try copying and running that code on your system. You just have to click on a button to change it's color. (right now it will change it's color to '#f0f' which \think translates to pink.) I will try to edit it make it red ('#f00') – P S Solanki Oct 05 '20 at 12:45
  • 1
    I did copied it and tried to run but when im clicking it it doesn't change the colour – shalini Oct 05 '20 at 13:34
  • @shalini Hmm. It works fine on my machine xD. see [HERE](https://drive.google.com/file/d/1B7F3hNB_FArFAN6j4tDdDNCa9t9tk43l/view?usp=sharing) to have a look how it looks on my machine. You might wanna ignore the other elements in the screen capture. I only have one screen and I couldn't close out the window :) . Regardless of that, are you on a Mac (although that should not make a difference) ?? – P S Solanki Oct 05 '20 at 14:32
  • 1
    @P S Solanki yes I'm on a Mac . I'm currently using IDLE 3.7.4. I don't think that would make a difference but it is still not happening even if I'm just copying your code and running it . – shalini Oct 05 '20 at 15:53
  • 1
    @shalini interesting.. Try [THIS](https://paste.atilla.org/paste/07RGSC) code and tell me if it changes color ON `clicking` the button. Also by any chance you are using `Tkinter` (not `tkinter`) (the one with `Python2`) because Py2 comes pre-installed in Mac devices? – P S Solanki Oct 05 '20 at 17:38
  • 1
    no nothing is happening if I click on the test button . No I'm using tkinter . – shalini Oct 07 '20 at 03:18
  • 1
    I got the solution I have to use highlight background instead of bg – shalini Oct 07 '20 at 03:41
  • 1
    but is it important to give the command for each button or I can use as a function also and if yes , how can write that because I have different names for all the buttons – shalini Oct 07 '20 at 04:00
  • 1
    @shalini Great fo know that. I don't own a Mac so I wasn't sure. As far as your question about `command` is concerned, it is necessary for each button to have a command `argument`. because `tkinter` uses event handling to manage user events and the button click event has to trigger some function in order to do something. You can write a function though but you will need to pass some argument to let it know which button was pressed. check my next comment for a sample code. – P S Solanki Oct 07 '20 at 04:28
  • 1
    @shalini [Click here](https://paste.atilla.org/paste/4BPKYI) for a sample code using functions. I will try to see if I can add that to my answer if I find some time. Moreover it is customary on stackoverflow to upvote an answer if ONLY it was helpful and also accept an answer if it solved your issue. You can do that anytime from top left corner of an answer thread. – P S Solanki Oct 07 '20 at 04:38
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/222631/discussion-between-shalini-and-p-s-solanki). – shalini Oct 07 '20 at 04:43
  • @shalini sure, you need to join the chatroom by clicking on link you created. Notice that chat rooms are public and anyone can read the discussions. however the room creator can decide who can participate in the discussion. – P S Solanki Oct 07 '20 at 04:47