尝试配置按钮,以便在单击后更改颜色。
movieSeats=Button(root, text="Empty", bg="green", fg="white", width=5,
height=1, command=lambda c=c, r=r:[redClick(c, r)])
File "C:/Users/----/Downloads/test2.py", line 21, in redClick
movieSeats.configure(bg="red")
NameError: name 'movieSeats' is not defined
我希望它能改变颜色 这是错误:
File "C:\Users\Brain\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\freezer.py", line 750, in __init__
parts = version.split(".")
AttributeError: 'NoneType' object has no attribute 'split' "
答案 0 :(得分:1)
movieSeats
是buttonsMake()
中的局部变量,因此redClick
中不存在,您会收到错误name 'movieSeats' is not defined
您必须在global movieSeats
中使用buttonsMake()
来创建全局变量。
<强>顺便说一句:强>
您将所有按钮分配到同一个变量,这样您只能访问最后一个按钮。您可以将所有按钮保留在列表中,也可以将其作为参数
发送到redClick
import tkinter as tk
# --- functions ---
def make_buttons():
for c in range(10):
for r in range(3):
btn = tk.Button(root, text="Empty")
btn['command'] = lambda c=c, r=r, b=btn:red_click(c, r, b)
btn.grid(row=r,column=c)
def red_click(c, r, btn):
btn.configure(bg="red")
# --- main ---
root = tk.Tk()
make_buttons()
root.mainloop()