Conditional disabling of widget
-
08-03-2021 - |
Question
In the following code, I have been trying to disable the entry1 widget every time I select 1 in the combobox and disable entry2 when i select 2 in combobox.
this is my code:
from Tkinter import *
import ttk
def refresh():
if v.get() == 'A':
entry1.state(['disabled'])
entry2.state(['!disabled'])
elif v.get() == 'B':
entry2.state(['disabled'])
entry1.state(['!disabled'])
root = Tk()
v = StringVar()
var = StringVar()
entry1 = ttk.Entry (root, textvariable= var)
entry1.grid(row=2, column=2, sticky=(E,W))
entry2 = ttk.Entry (root, textvariable= var)
entry2.grid(row=4, column=2, sticky=(E,W))
v_list=['A','B']
v.set(v_list[1])
v_optionmenu = apply(OptionMenu, (root,v) + tuple(v_list))
v_optionmenu.grid(column=4,row=11,sticky=(W,E))
var = v
root.bind('<Return>', lambda e: refresh)
root.mainloop()
I wanted to create condition based enabling and disabling of widgets. My conditions usually are:
selection in combobox Selection of radiobutton
Please Advice how i can go about it.
Solution 2
def refresh(*args):
if v.get() == 'Disable' :
fb_entry.state(['disabled'])
fw_entry.state(['disabled'])
fb.set('')
fw.set('')
elif v.get() == 'Enable' :
fb_entry.state(['!disabled'])
fw_entry.state(['!disabled'])
root = Tk()
v = StringVar()
var = StringVar()
entry1 = ttk.Entry (root, textvariable= var)
entry1.grid(row=2, column=2, sticky=(E,W))
entry2 = ttk.Entry (root, textvariable= var)
entry2.grid(row=4, column=2, sticky=(E,W))
v_list=['Disable','Enable']
v.set(v_list[1])
v_optionmenu = apply(OptionMenu, (root,v) + tuple(v_list))
v_optionmenu.grid(column=4,row=11,sticky=(W,E))
var = v
root.bind('<Enter>',refresh_widget)
root.mainloop()
OTHER TIPS
The piece you seem to be missing is that you can change the state with the configure
method. Also, you can set a trace on a variable to have a function called when the value changes. Since you said you want to change the state based on a combobox, that is the technique you would use. You can do a similar trick with a radiobutton, though radiobuttons also have a command
option which can be used instead of a trace.
Here's an example showing how to trigger a "refresh" function when a combobox changes:
import Tkinter as tk
import ttk
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.choiceVar = tk.StringVar()
self.e1 = ttk.Entry(self)
self.e2 = ttk.Entry(self)
self.cb = ttk.Combobox(self, textvariable=self.choiceVar,
values=["Enable 1 only", "Enable 2 only"])
self.cb.set(self.cb.cget("values")[0])
self.cb.pack(side="top")
self.e1.pack(side="top")
self.e2.pack(side="top")
self.choiceVar.trace("w", self.on_trace_choice)
self.refresh()
def on_trace_choice(self, name, index, mode):
self.refresh()
def refresh(self):
choice = self.cb.get()
if choice == "Enable 1 only":
self.e1.configure(state="normal")
self.e2.configure(state="disabled")
else:
self.e1.configure(state="disabled")
self.e2.configure(state="normal")
if __name__ == "__main__":
app = SampleApp()
app.mainloop()