tkinter>>如何获得按钮参考? [英] tkinter >> how to get button references?

查看:48
本文介绍了tkinter>>如何获得按钮参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用Python和tkinter创建战舰游戏.我需要声明100个按钮(每个玩家可以触发的位置一个按钮),我发现我可以这样做以将它们放置在10x10的网格中,同时还使用变量button_dict存储对它们的引用

I am currently trying to create a battleship game with Python and tkinter. I need to declare 100 buttons (one for each spot that a player can fire on), and I figured out that I could do this to get them in a 10x10 grid while also storing a reference to them with the variable buttons_dict

from tkinter import *
from tkinter import ttk

root = Tk()
board_frame = ttk.Frame(root, padding=5)
board_frame.grid(column=0,row=0)

COORDS_LIST = []
buttons_dict = {}

for r in range(1,11):
    for c in range(1,11):
        coord = str(r)+"_"+str(c)
        COORDS_LIST.append(coord)
        buttons_dict[COORDS_LIST[-1]] = ttk.Button(board_frame, text="O", width="2")
        buttons_dict[COORDS_LIST[-1]].grid(row=r,column=c)

root.mainloop()

此代码创建了100个完全按照我的需要显示的按钮.当我想给每个命令一个问题时,问题就开始了.我想要的是一个通用函数(对于这个问题,我们可以称其为"fire_here"),每次按下其中一个按钮时都会调用该函数,但是我需要将其传递给1或2个参数:它可以是创建该按钮时附加到COORDS_LIST的值,也可以是该按钮的列"和行"值.那么我该怎么做?或者一旦放置一个按钮,如何获得它的行/列?

This code creates 100 buttons displayed exactly as I need. The problem begins when I want to give a command to each one of them. What I would want is a general function (we can call it 'fire_here' for this question) that is called every time one of the buttons is pressed, but I need it to be passed either 1 or 2 arguments: It can be the value that was appended to the COORDS_LIST when that button was created, or it can be the 'column' and 'row' values of the button. So how do I do that OR how do I get the row/column of one button once it was placed??

推荐答案

我认为您想要这样的东西:

I think you want something like this:

from tkinter import *
from tkinter import ttk

root = Tk()
board_frame = ttk.Frame(root, padding=5)
board_frame.grid(column=0,row=0)

COORDS_LIST = []
buttons_dict = {}

###########################################
def fire_here(x, y):
    print("column:{}, row:{}".format(x, y))
###########################################

for r in range(1,11):
    for c in range(1,11):
        coord = str(r)+"_"+str(c)
        COORDS_LIST.append(coord)
        buttons_dict[COORDS_LIST[-1]] = ttk.Button(board_frame, text="O", width="2")
        ###########################################################################
        buttons_dict[COORDS_LIST[-1]]["command"] = lambda x=c, y=r: fire_here(x, y)
        ###########################################################################
        buttons_dict[COORDS_LIST[-1]].grid(row=r,column=c)

root.mainloop()

我添加的所有内容都在注释框中.但是,重要的是:

All of the stuff I added is in comment boxes. However, the important part is this:

buttons_dict[COORDS_LIST[-1]]["command"] = lambda x=c, y=r: fire_here(x, y)

基本上,它有两件事:

  1. 使用 lambda ,它将为当前按钮的 command 选项创建一个函数.单击该按钮时将调用此函数.

  1. Using lambda, it creates a function for the current button's command option. This function will be called when the button is clicked.

它获取当前的列和行(网格上按钮的坐标),并将其设置为默认值,作为 x y 参数的默认值.功能.此外,在调用该函数时,这些值将被发送到 fire_here 进行处理.

It takes the current column and row (the button's coordinates on the grid) and sets them as the default values for the x and y parameters of the function. Moreover, when the function is called, those values will be sent to fire_here for processing.


此外,您可以将其放在上面的行中,如下所示:


Also, you could put that on the line above like so:

buttons_dict[COORDS_LIST[-1]] = ttk.Button(board_frame, text="O", width="2", command=lambda x=c, y=r: fire_here(x, y))

但是,我将其放在一行上以提高代码的清晰度.

However, I put it on its own line to improve the code's clarity.

这篇关于tkinter>>如何获得按钮参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆