如何在 tkinter 中禁用绑定到其自身框架的其他两个单选按钮

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

我正在使用 tkinter 设计 GUI。三个单选按钮绑定到每个单独的框架。是否可以通过单击其中一个来禁用其他两个单选按钮?或者甚至更好地禁用其他两个框架

我无法实现此功能。下面是部分代码。如果您需要更多信息,请告诉我。谢谢

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()

是的,你可以,像这样:

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)

但这有一个很大的问题。如果禁用其他 2 个单选按钮,用户将无法再点击它们。所以第一次点击他们就承诺了。

您需要重新考虑您的设计。我认为 ttk.Notebook 在这里更有意义。