13

I want to change ttk.Button's state according to some internal logic. I create a button and associate a style with it:

cardBtnStyle = ttk.Style()
cardBtnStyle.configure('CB.TButton')
cardBtn = ttk.Button(top, text="Make SD card", style='CB.TButton', command = cardCreateCallBack).grid(column=1, row=5)

Following statement has no effect:

style.configure('CB.TButton', state='disabled')

But when I create a button like this, it is disabled:

cardBtn = ttk.Button(top, text="Make SD card", style='CB.TButton', state='disabled', command = cardCreateCallBack).grid(column=1, row=5)

How do I change ttk.Button state in Python?

OS: Ubuntu 13.10

Python: 2.7.5+

yegorich
  • 4,653
  • 3
  • 31
  • 37

1 Answers1

23

The button state is not part of its style. You can use the state() method to modify it:

cardBtn.state(["disabled"])   # Disable the button.
cardBtn.state(["!disabled"])  # Enable the button.
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • I get following error: Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1473, in __call__ return self.func(*args) File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 534, in callit func(*args) File "./viaVPN-Production.py", line 42, in state_thread cardBtn.state(["disabled"]) AttributeError: 'NoneType' object has no attribute 'state' – yegorich Feb 10 '14 at 11:41
  • 1
    @yegorich, that error message means that `cardBtn` is set to `None`. However, the code in your question assigns an instance of `ttk.Button` to it, so it should not be the case. Double-check that you're invoking `state()` after initializing the button instance. – Frédéric Hamidi Feb 10 '14 at 12:32
  • 3
    I think I've found the problem: you can't create an object, if you place it at once (`.grid()` or `.pack()`). It must be done in two stages: create an object and then place it. This way you can use its handle. – yegorich Feb 10 '14 at 21:06