Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 网格上的TkInter按钮坐标_Python_User Interface_Button_Tkinter_Grid - Fatal编程技术网

Python 网格上的TkInter按钮坐标

Python 网格上的TkInter按钮坐标,python,user-interface,button,tkinter,grid,Python,User Interface,Button,Tkinter,Grid,我目前正在用TkInter制作一个GUI,GUI是由一个构建在for循环中的网格组成的,所有按钮都有一个变量(每个循环都会更新)。 我想知道怎样才能得到我按下的任何按钮的坐标。(例如,当我按下按钮时,按钮给出了它在网格上的坐标)我使用了网格信息,但它只获取创建的最后一个按钮的信息。 感谢您的帮助:)您可以将行和列传递给与函数关联的命令。最常用的两种方法是使用lambda或functools.partial 下面是一个使用lambda的示例: import tkinter as tk def c

我目前正在用TkInter制作一个GUI,GUI是由一个构建在for循环中的网格组成的,所有按钮都有一个变量(每个循环都会更新)。 我想知道怎样才能得到我按下的任何按钮的坐标。(例如,当我按下按钮时,按钮给出了它在网格上的坐标)我使用了网格信息,但它只获取创建的最后一个按钮的信息。
感谢您的帮助:)

您可以将行和列传递给与函数关联的命令。最常用的两种方法是使用lambda或functools.partial

下面是一个使用lambda的示例:

import tkinter as tk

def click(row, col):
    label.configure(text="you clicked row %s column %s" % (row, col))

root = tk.Tk()
for row in range(10):
    for col in range(10):
        button = tk.Button(root, text="%s,%s" % (row, col), 
                           command=lambda row=row, col=col: click(row, col))
        button.grid(row=row, column=col, sticky="nsew")
label = tk.Label(root, text="")
label.grid(row=10, column=0, columnspan=10, sticky="new")

root.grid_rowconfigure(10, weight=1)
root.grid_columnconfigure(10, weight=1)

root.mainloop()

可以将行和列传递给与函数关联的命令。最常用的两种方法是使用lambda或functools.partial

下面是一个使用lambda的示例:

import tkinter as tk

def click(row, col):
    label.configure(text="you clicked row %s column %s" % (row, col))

root = tk.Tk()
for row in range(10):
    for col in range(10):
        button = tk.Button(root, text="%s,%s" % (row, col), 
                           command=lambda row=row, col=col: click(row, col))
        button.grid(row=row, column=col, sticky="nsew")
label = tk.Label(root, text="")
label.grid(row=10, column=0, columnspan=10, sticky="new")

root.grid_rowconfigure(10, weight=1)
root.grid_columnconfigure(10, weight=1)

root.mainloop()