当从最后一个点击时,按钮不会变为完全

时间:2017-12-13 16:31:44

标签: python python-3.x tkinter

我希望所有按钮在单击时更改为完整,但我只能使用最后一个按钮。这是我的代码,但我不能让其他人工作。帮助将不胜感激。

from tkinter import*
root = Tk()
root.geometry("500x500")
root.resizable (0, 0)


def button():
    for x in range(5):
        for y in range(5):


seat2=Button(root,text="empty",bg="green",fg="black",height=5,width=10)
            seat2['command']=lambda s1=seat1:(toggle_text(s1))
            seat2.grid(row=x, column=y)
button()



def toggle_text(s1):
    """toggle Button text between empty and Full"""
    if s1["text"]== "empty":
        #switch to Full
        s1["text"]= "Full"

1 个答案:

答案 0 :(得分:1)

以下代码有效。我没有重构代码来改进它,但它似乎做了你期望的。

from tkinter import*
root = Tk()
root.geometry("500x500")
root.resizable (0, 0)

def toggle_text(s1):
    """toggle Button text between empty and Full"""
    if s1["text"] == "empty":
        #switch to Full
        s1["text"] = "Full"
    else:
        s1["text"] = 'empty'

def button():
    for x in range(5):
        for y in range(5):
            seat2=Button(root,text="empty",bg="green",fg="black",height=5,width=10)
            seat2['command']=lambda x=seat2: toggle_text(x)
            seat2.grid(row=x, column=y)


button()