Disabling buttons after click in Tkinter

笑着哭i 提交于 2020-12-29 07:17:04

问题


I'm new to Python and I'm trying to make a simple application using Tkinter.

def appear(x):
    return lambda: results.insert(END, x)

letters=["A", "T", "D", "M", "E", "A", "S", "R", "M"] 

for index in range(9): 
    n=letters[index] 
    nButton = Button(buttons, bg="White", text=n, width=5, height=1,
    command =appear(n), relief=GROOVE).grid(padx=2, pady=2, row=index%3,
    column=index/3)

What I'm trying to do is disable the buttons once I click them. I tried

def appear(x):
    nButton.config(state="disabled")
    return lambda: results.insert(END, x)

But it gives me the following error:

NameError: global name 'nButton' is not defined


回答1:


There are a few issues here:

  1. Whenever you create widgets dynamically, you need to store references to them in a collection so that you can access them later.

  2. The grid method of a Tkinter widget always returns None. So, you need to put any calls to grid on their own line.

  3. Whenever you assign a button's command option to a function that requires arguments, you must use a lambda or such to "hide" that call of the function until the button is clicked. For more information, see https://stackoverflow.com/a/20556892/2555451.

Below is a sample script addressing all of these issues:

from Tkinter import Tk, Button, GROOVE

root = Tk()

def appear(index, letter):
    # This line would be where you insert the letter in the textbox
    print letter

    # Disable the button by index
    buttons[index].config(state="disabled")

letters=["A", "T", "D", "M", "E", "A", "S", "R", "M"]

# A collection (list) to hold the references to the buttons created below
buttons = []

for index in range(9): 
    n=letters[index]

    button = Button(root, bg="White", text=n, width=5, height=1, relief=GROOVE,
                    command=lambda index=index, n=n: appear(index, n))

    # Add the button to the window
    button.grid(padx=2, pady=2, row=index%3, column=index/3)

    # Add a reference to the button to 'buttons'
    buttons.append(button)

root.mainloop()



回答2:


This was very helpful for a work I'm currently working on, to add a minor correction

from math import floor



button.grid(padx=2, pady=2, row=index%3, column=floor(index/3))


来源:https://stackoverflow.com/questions/20596892/disabling-buttons-after-click-in-tkinter

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