Tkinter Buttons Disabled/Enabled on click

Viewed 7151

For the code I am writing I am trying to set it up so that in a Tkinter UI, when you press a button, the button becomes disabled, and when you press a button while another button is disabled, the currently disabled button becomes enabled again.

import tkinter as tk
from tkinter import ttk

def btnPressed(index):
    print("Button {0} disabled".format(index))
    for i in range(len(buttons)):
        print("Button {0}, State: {1}".format(index, buttons[index]['state']))
        if buttons[i]['state'] == 'disabled':
            buttons[i].configure(state = 'normal')
    buttons[index].configure(state = 'disabled')


root = tk.Tk()
buttons = []
btn1 = ttk.Button(root, text = "Button 1", command = lambda: btnPressed(0))
btn1.pack()
buttons.append(btn1)
btn2 = ttk.Button(root, text = "Button 2", command = lambda: btnPressed(1))
btn2.pack()
buttons.append(btn2)
btn3 = ttk.Button(root, text = "Button 3", command = lambda: btnPressed(2))
btn2.pack()
buttons.append(btn3)
root.mainloop()

Right now I can get it so that the buttons are disabling themselves, but then will not go back to their 'normal' state. I tried doing some bug testing putting

print("Button {0}, State: {1}".format(index, buttons[index]['state']))

in the for loop so I could see every Buttons current state before it checked the if statement.

Every button, while on the UI is getting disabled, still had a state value of 'normal'. So obviously something is wrong, but I am not sure what that something is.

2 Answers
Related