Python: ttk: disable/enable a button

陌路散爱 提交于 2019-12-06 22:34:02

问题


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+


回答1:


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.


来源:https://stackoverflow.com/questions/21673257/python-ttk-disable-enable-a-button

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!