Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: ttk: disable/enable a button

Tags:

python

ttk

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+

like image 605
yegorich Avatar asked Feb 10 '14 09:02

yegorich


People also ask

How do I enable and disable a button in Python?

Build A Paint Program With TKinter and Python Tkinter Button widgets can be enabled and disabled by defining its state in the Button Object. The state attribute generally accepts two values Normal and Disabled which are used for enabling and disabling the button, respectively.

Is TTK better than Tkinter?

Tkinter widgets are used to add Buttons, Labels, Text, ScrollBar, etc., however, tkinter. ttk supports a variety of widgets as compared to tkinter widgets. Tkinter. ttk doesn't support Place, Pack() and Grid(), thus it is recommended to use tkinter widget with ttk.


1 Answers

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.
like image 74
Frédéric Hamidi Avatar answered Nov 12 '22 03:11

Frédéric Hamidi