1

I made a simple window with some buttons usin Tkinter. One button is in the disabled state at the begining, but I want it to turn into a normal state again after I press another button. So how can I change the button's properties while the code is running?

from tkinter import *

def com():
    print ('activate')

def win():
    window = Tk()
    window.geometry ('500x500')
    b1 = Button(text = 'Disabled', pady = '10', padx = '10', state = DISABLED)
    b1.place(x=100, y=10)
    b2 = Button(text = 'activate', pady = '10', padx = '10', command=com)
    b2.place(x=10, y=10)
    window.mainloop

win()  
prok_8
  • 309
  • 2
  • 15

1 Answers1

1

you must use the <buttonname>.config(state=STATE) command, with STATE being either:

  • NORMAL (default)
  • ACTIVE
  • DISABLED

Source I took the liberty of creating editting your code to test this, and it does work, see below!

from Tkinter import *

def com1():
    print ('activate')
    b1.config(state = ACTIVE)
    b2.config(state = DISABLED) #toggles the buttons

def com2():
    print('de-activate')
    b1.config(state = DISABLED)
    b2.config(state = ACTIVE) #toggles the buttons

def win():
    global b1
    global b2 #This is to allow passing b1 and b2 to com1 and com2, but may not be the most "efficient" way to do this...
    window = Tk()
    window.geometry ('500x500')
    b1 = Button(text = 'Disabled', pady = '10', padx = '10', state = DISABLED, command = com2) #command com2 added here
    b1.place(x = 100, y = 10)
    b2 = Button(text = 'activate', pady = '10', padx = '10', command = com1) #com changed to com1
    b2.place(x = 10, y = 10)

    window.mainloop()

win()  
Matthew
  • 672
  • 4
  • 11
  • Thank you. And I apologize for the fault title. I wanted to ask a question with that title a while ago but I found the solution myself, but the title stayed there and I forgot to changed it. – prok_8 Dec 07 '14 at 14:17