Two-button color toggle

I am trying to make two buttons “toggle”, where one is green and the other red, the green one being the last button pressed. LowerTight is already defined earlier in the code as True to start. What’s the best way to make the boolean change with the button press?

   # Button for loosen lumbar
       
        self.button_loosen_lumbar = tk.Button(master,text ="Loosen Lumbar" , width=20, height=12, bg="#800000")
        self.button_loosen_lumbar.grid(row=1, column=0, padx=14, pady=10)
        self.button_loosen_lumbar.bind("<ButtonPress>", self.on_press_loosen_lumbar)
        
        if lowerTight:
            self.button_loosen_lumbar.configure(bg="#800000")
        else:
            self.button_loosen_lumbar.configure(bg="#404040")


        # Button for tighten lumbar

        self.button_tighten_lumbar = tk.Button(master, text = "Tension Lumbar", width=20, height=12, bg="#800000")
        self.button_tighten_lumbar.grid(row=1, column=1, padx=14, pady=10)
        self.button_tighten_lumbar.bind("<ButtonPress>", self.on_press_tighten_lumbar)

        if lowerTight:
            self.button_tighten_lumbar.configure(bg="#404040")
        else:
            self.button_tighten_lumbar.configure(bg="#800000")
        


Just change the colours when a button is pressed:

import tkinter as tk

class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)

        self.lowerTight = True

        # Button for loosen lumbar
        self.button_loosen_lumbar = tk.Button(self, text ="Loosen Lumbar" , width=20, height=12)
        self.button_loosen_lumbar.grid(row=1, column=0, padx=14, pady=10)
        self.button_loosen_lumbar.bind("<ButtonPress>", self.on_press_loosen_lumbar)

        # Button for tighten lumbar
        self.button_tighten_lumbar = tk.Button(self, text = "Tension Lumbar", width=20, height=12)
        self.button_tighten_lumbar.grid(row=1, column=1, padx=14, pady=10)
        self.button_tighten_lumbar.bind("<ButtonPress>", self.on_press_tighten_lumbar)

        self.colour_buttons()

    def on_press_loosen_lumbar(self, *args):
        self.lowerTight = True
        self.colour_buttons()

    def on_press_tighten_lumbar(self, *args):
        self.lowerTight = False
        self.colour_buttons()

    def colour_buttons(self):
        RED = "#800000"
        GREEN = "#008000"

        if self.lowerTight:
            self.button_loosen_lumbar.configure(bg=GREEN)
            self.button_tighten_lumbar.configure(bg=RED)
        else:
            self.button_loosen_lumbar.configure(bg=RED)
            self.button_tighten_lumbar.configure(bg=GREEN)

App().mainloop()
1 Like

To be clear: you mean that if the red button is pressed, it should become green, while the other one becomes red? And if the green button is pressed, nothing should change (visually at least)?

Thanks for the help all, color requirements changed but I ended up with something like this:
(I’ll just show the code chunks for a “pair” of buttons. Assume I already have the two buttons “made”, as I did all my work in the functions activated by the button.

 def on_press_loosen_upper(self, event):
       self.send_serial_command('D')
       self.upperTight= False
       self.ColorUp()

    def on_press_tighten_upper(self, event):
        self.send_serial_command('M')
        self.upperTight= True
        self.ColorUp()

    def ColorUp(self):
        RED = "#800000"
        WHITE = "white"

        if self.upperTight:
            self.button_loosen_upper.configure(bg=RED)
            self.button_tighten_upper.configure(bg=WHITE)
        else:
            self.button_loosen_upper.configure(bg=WHITE)
            self.button_tighten_upper.configure(bg=RED)

Just leaving this here in case someone else wants it.

For those curious, the Serial Send is communicating to an Arduino which is the “motor controller” for the project.