我正在尝试在tkinter中创建一个游戏板应用程序。我在画布上有5行(1-5)和6列(0-6)按钮。有没有办法让按下按钮的行和列整数值可以在assign_Func中访问,按下按钮时会调用它?我想将gameboard [row] [column]分配给按下的按钮的索引。
我当前的代码会产生此错误: 第31行,在assign_Func中 gameboard [row] [column] = 11 NameError:未定义全局名称“row”
from Tkinter import *
from PIL import ImageTk, Image
from Tkinter import Button
gameboard = ([[0,1,2,3,4,5,6],
[0,1,2,3,4,5,6],
[0,1,2,3,4,5,6],
[0,1,2,3,4,5,6],
[0,1,2,3,4,5,6]])
i = 0
#Essential code
root = Tk()
frame = Frame(root, background="blue")
frame.pack(expand = TRUE)
window = Tk()
#Supposed to assign the row and column of the pressed button to the row and column of gameboard.
def assign_Func():
global gameboard
global i
while i < 5:
gameboard[row][column] = 11
i = i + 1
#Create buttons in rows 1-5, columns 0-6
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=1, column=0, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=2, column=0, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=3, column=0, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=4, column=0, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=5, column=0, padx=5, pady=5)
#column 1
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=1, column=1, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=2, column=1, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=3, column=1, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=4, column=1, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=5, column=1, padx=5, pady=5)
# column 2
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=1, column=2, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=2, column=2, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=3, column=2, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=4, column=2, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=5, column=2, padx=5, pady=5)
# column 3
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=1, column=3, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=2, column=3, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=3, column=3, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=4, column=3, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=5, column=3, padx=5, pady=5)
# column 4
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=1, column=4, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=2, column=4, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=3, column=4, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=4, column=4, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=5, column=4, padx=5, pady=5)
# column 5
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=1, column=5, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=2, column=5, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=3, column=5, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=4, column=5, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=5, column=5, padx=5, pady=5)
#column 6
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=1, column=6, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=2, column=6, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=3, column=6, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=4, column=6, padx=5, pady=5)
watermark = Button(frame, background = "red", text="~", fg="black", command=assign_Func)
watermark.grid(row=5, column=6, padx=5, pady=5)
root.mainloop()
答案 0 :(得分:0)
您可以使用lambda
在command=
Button(..., command=lambda:assign_Func(1, 1) )
和assign_Func
必须得到它
def assign_Func(row, column):
如果您使用for
循环创建Buttons
,则必须为lambda
command=lambda r=row,c=col:assign_Func(r, c)
在for
循环中
for row in range(1, 6):
for col in range(0, 7):
b = Button( ... , command=lambda r=row, c=col:assign_Func(r, c))
b.grid(row=row, column=col, ...)