Search code examples
pythontkinterradio-buttondisable

how to disable the other two radiobuttons which bind to its own frame in tkinter


I am designing a GUI with tkinter. There are three radio-buttons bind to each individual frame. Is it a way to disable the other two radio-buttons by clicking one of them? Or even better to disable the other two frameenter image description here

I am not able to achieve this feature. Below is part of the code. Please let me know if you need more information. Thanks

root = Tk()
root.title("MyApp")   

f1 = ttk.Frame(root, padding="3 3 12 12")
f1.grid(row=0, sticky=(W, E, N, S))
Label(f1, text = "Welcome to My App!", font=("Times New Roman", 20)).grid(column=3, row=1, sticky='EW')  


root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)

option = StringVar()

topframe=ttk.Frame(root, padding="3 3 12 12", relief = GROOVE, borderwidth = 10)
topframe.grid(row=1, sticky=(W, E, N, S))

mainframe = ttk.Frame(root, padding="3 3 12 12",  relief = GROOVE, borderwidth = 10)
mainframe.grid(row=2, sticky=(W, E, N, S))

bottomframe = ttk.Frame(root, padding="3 3 12 12", relief = GROOVE, borderwidth = 10)
bottomframe.grid(row=3, sticky=(W, E, N, S))

paraframe = ttk.Frame(root, padding="3 3 12 12" , relief = GROOVE, borderwidth = 10)
paraframe.grid(row=4, sticky=(W, E, N, S))



rb1 = Radiobutton(topframe, text="Select Files", value="files", var=option)
rb1.grid(column=0, columnspan=2, row=0, sticky=W)

rb2 = Radiobutton(mainframe, text="Select a Directory", value="directory", var=option)
rb2.grid(column=0,columnspan=2, row=0,sticky=W)

rb3 = Radiobutton(bottomframe, text="Paste a JSON File", value="json", var=option)
rb3.grid(column=0,columnspan=2, row=0,sticky=W)

# function to gray out
def greyOutNotTop():
    if option.get() == "files":
        rb2.config(state='disable')
        rb3.config(state='disable')

def greyOutNotMain():
    if option.get() == "directory":
        rb1.config(state='disable')
        rb3.config(state='disable')
def greyOutNotBottom():
    if option.get() == "json":
        rb1.config(state='disable')
        rb2.config(state='disable')

root.mainloop()

Solution

  • Yes, you can, like this:

    def greyOutNotTop():
        rb2.config(state='disable')
        rb3.config(state='disable')
    
    rb1 = Radiobutton(topframe, text="Select Files", value="files", var=option, command =greyOutNotTop)
    rb1.grid(column=0, columnspan=2, row=0, sticky=W)
    

    But there's a huge problem with that. If you disable the other 2 radiobuttons, the user can't click on them anymore. So with the first click they are committed.

    You need to rethink your design. I think a ttk.Notebook would make a lot more sense here.