禁用Python和Tk中的按钮

禁用Python和Tk中的按钮,python,tkinter,tk,Python,Tkinter,Tk,学习小组 我正在使用一个gui,其中有一行单选按钮,必须在“自动”模式下禁用 脚本的一部分: # define Auto Manual Mode: def AM_procedure(): global manual if vam.get()==1: am_mode.set("Auto") manual = False for txt,val in space: space_button=Radiobut

学习小组

我正在使用一个gui,其中有一行单选按钮,必须在“自动”模式下禁用

脚本的一部分:

#  define Auto Manual Mode: 
def AM_procedure():
    global manual
    if vam.get()==1:
        am_mode.set("Auto")
        manual = False 
        for txt,val in space:
            space_button=Radiobutton(root, text = txt, value=val)
            space_button.configure(state = 'disabled')
    else:
        am_mode.set("Manual")
        manual = True
        for txt,val in space:
            space_button=Radiobutton(root, text = txt, value=val)
            space_button.configure(state='normal')
    print manual
    return;
剪断。。。 按钮:

for txt,val in space:
    space_button=Radiobutton(root, text = txt, font=fnt, indicatoron=0, width=8, variable=v, command=ShowChoice,value=val)
    space_button.grid(row=8, column=val-1)

for txt,val in AM:
    am_button=Radiobutton(root, text = txt, font=fnt, indicatoron=0, width=8, variable=vam, command=AM_procedure, value=val)
    am_button.grid(row=9, column=val+1)
def create_space_buttons():
    while space_buttons: #same as while len(space_buttons)>0:
        space_buttons.pop().destroy() #remove existing buttons

    for txt,val in space:
        space_button=Radiobutton(root, text = txt, font=fnt, indicatoron=0, width=8, variable=v, command=ShowChoice,value=val)
        space_button.grid(row=8, column=val-1)
        #add it to the list of space buttons
        space_buttons.append(space_button)
空间包含5个元组,AM 2。除其他事项外,按下自动按钮应使第一行变灰。我试了几次,最后一个按钮都变成灰色了。但是这个脚本没有任何效果。 请告知。 提前感谢,,
Harke

创建这样的小部件的问题:

for i in range(10):
    button=Button(root, text = i)
    button.grid(column=i)
一旦
for
循环完成,就没有简单的方法访问创建的按钮,仍然有一个变量
button
将指向最后创建的小部件,您可以使用
root.winfo_children()
获取根的每个子项并配置所有子项:

for w in root.winfo_children():
    w.configure(state="disabled")
但是如果你在根窗口上运行它,它会禁用你程序中几乎所有的东西

您真正需要的是一种保留对您创建的所有按钮的引用的方法:

space_buttons = [] #list of space buttons

...

for txt,val in space:
    space_button=Radiobutton(root, text = txt, font=fnt, indicatoron=0, width=8, variable=v, command=ShowChoice,value=val)
    space_button.grid(row=8, column=val-1)
    #add it to the list of space buttons
    space_buttons.append(space_button)
然后,当您想要配置它们时,您可以使用:

for button in space_buttons:
    button.configure(state="disabled")
唯一的潜在问题是,如果程序中的
空格
列表在某个点发生更改,则需要一个函数来重新创建按钮:

for txt,val in space:
    space_button=Radiobutton(root, text = txt, font=fnt, indicatoron=0, width=8, variable=v, command=ShowChoice,value=val)
    space_button.grid(row=8, column=val-1)

for txt,val in AM:
    am_button=Radiobutton(root, text = txt, font=fnt, indicatoron=0, width=8, variable=vam, command=AM_procedure, value=val)
    am_button.grid(row=9, column=val+1)
def create_space_buttons():
    while space_buttons: #same as while len(space_buttons)>0:
        space_buttons.pop().destroy() #remove existing buttons

    for txt,val in space:
        space_button=Radiobutton(root, text = txt, font=fnt, indicatoron=0, width=8, variable=v, command=ShowChoice,value=val)
        space_button.grid(row=8, column=val-1)
        #add it to the list of space buttons
        space_buttons.append(space_button)

对于
am_按钮

可能还有一个类似的功能,用于解决创建如下小部件的问题:

for i in range(10):
    button=Button(root, text = i)
    button.grid(column=i)
一旦
for
循环完成,就没有简单的方法访问创建的按钮,仍然有一个变量
button
将指向最后创建的小部件,您可以使用
root.winfo_children()
获取根的每个子项并配置所有子项:

for w in root.winfo_children():
    w.configure(state="disabled")
但是如果你在根窗口上运行它,它会禁用你程序中几乎所有的东西

您真正需要的是一种保留对您创建的所有按钮的引用的方法:

space_buttons = [] #list of space buttons

...

for txt,val in space:
    space_button=Radiobutton(root, text = txt, font=fnt, indicatoron=0, width=8, variable=v, command=ShowChoice,value=val)
    space_button.grid(row=8, column=val-1)
    #add it to the list of space buttons
    space_buttons.append(space_button)
然后,当您想要配置它们时,您可以使用:

for button in space_buttons:
    button.configure(state="disabled")
唯一的潜在问题是,如果程序中的
空格
列表在某个点发生更改,则需要一个函数来重新创建按钮:

for txt,val in space:
    space_button=Radiobutton(root, text = txt, font=fnt, indicatoron=0, width=8, variable=v, command=ShowChoice,value=val)
    space_button.grid(row=8, column=val-1)

for txt,val in AM:
    am_button=Radiobutton(root, text = txt, font=fnt, indicatoron=0, width=8, variable=vam, command=AM_procedure, value=val)
    am_button.grid(row=9, column=val+1)
def create_space_buttons():
    while space_buttons: #same as while len(space_buttons)>0:
        space_buttons.pop().destroy() #remove existing buttons

    for txt,val in space:
        space_button=Radiobutton(root, text = txt, font=fnt, indicatoron=0, width=8, variable=v, command=ShowChoice,value=val)
        space_button.grid(row=8, column=val-1)
        #add it to the list of space buttons
        space_buttons.append(space_button)

可能还有一个类似的函数用于
am_按钮

我很确定我正确地格式化了您的代码块,但是请随意编辑您的帖子,并使用
{}
按钮或control-k缩进代码块。如果我没有完全正确:)您创建了一组单选按钮和
grid()
将它们放在屏幕上,但在AM_过程中,您创建了一组不同的按钮,这些按钮从未放在GUI上,我认为您只需要保留对原始小部件的引用并对其进行配置,而不是创建从未显示的新小部件。我想您是对的。但是我如何引用这五个按钮而不是生成新的按钮呢?对不起,是的,我正处于学习阶段……不要期望“学习阶段”结束,对我来说肯定还没有结束,我认为这将像
单选按钮=[]一样简单。。。单选按钮。附加(空格按钮)。。。对于单选按钮中的b:b.grid(…)
,但我不完全确定在您的程序中实现它的最佳方式。谢谢您,Tadhg。我试图理解你的建议,但我不太明白。请你再具体一点好吗?同时,我在两组单选按钮周围构建了LabelFrame,看起来很不错。不幸的是,state选项不适用于LabelFrame,因此不幸的是,它不适用于它的子对象。提前谢谢!我很确定我正确地格式化了你的代码块,但是请随意编辑你的帖子,并使用
{}
按钮或control-k缩进代码块。如果我没有完全正确:)您创建了一组单选按钮和
grid()
将它们放在屏幕上,但在AM_过程中,您创建了一组不同的按钮,这些按钮从未放在GUI上,我认为您只需要保留对原始小部件的引用并对其进行配置,而不是创建从未显示的新小部件。我想您是对的。但是我如何引用这五个按钮而不是生成新的按钮呢?对不起,是的,我正处于学习阶段……不要期望“学习阶段”结束,对我来说肯定还没有结束,我认为这将像
单选按钮=[]一样简单。。。单选按钮。附加(空格按钮)。。。对于单选按钮中的b:b.grid(…)
,但我不完全确定在您的程序中实现它的最佳方式。谢谢您,Tadhg。我试图理解你的建议,但我不太明白。请你再具体一点好吗?同时,我在两组单选按钮周围构建了LabelFrame,看起来很不错。不幸的是,state选项不适用于LabelFrame,因此不幸的是,它不适用于它的子对象。提前谢谢!就这样!!!!!非常感谢您对解决方案的清晰描述以及您花时间来帮助我。再次非常感谢!就这样!!!!!非常感谢您对解决方案的清晰描述以及您花时间来帮助我。再次非常感谢!借据