在网格上查找小部件(tkinter 模块) [英] finding widgets on a grid (tkinter module)

查看:29
本文介绍了在网格上查找小部件(tkinter 模块)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 tkinter 模块,假设我创建了一个包含 50 个按钮小部件的网格,并且每个小部件都有不同的文本.我需要能够指定在行和列中输入的某种方式,并且我可以在该位置获取该小部件的文本.例如,如果我需要网格第二列第三行的小部件文本.我已经搜索了文档,但它告诉我如何在需要有关网格的信息时获取有关小部件的信息.

Using the tkinter module, suppose I create a grid with 50 button widgets and each of those widgets have different text. I need to be able to specify some way of typing in a row and column and I can get that widget's text at that location. So for example, if I need the widget's text at the third row in the second column of the grid. I've searched the docs but that tells me how to get info about widgets, when I need info about the grid.

提前致谢

推荐答案

您有一个 以前的答案 与将按钮对象保存在字典中以便使用它们在网格中的(列、行)位置恢复它们的方法相关的方法.

You got a previous answer relative to a method to save button objects in a dictionary in order to recover them using their (column, row) position in a grid.

因此,如果 self.mybuttons 是之前答案中描述的按钮列表字典,那么您可以在位置 row, col 处获取文本,如下所示:

So if self.mybuttons is your dictionary of lists of buttons as described in previous answer, then you can get the text at position row, col as this:

abutton = self.mybuttons[arow][acolumn]
text_at_row_col = abutton["text"]

另一方面,如果您需要从按钮回调中获取文本:

On the other hand, if what you need is to get the text from the button callback:

button.bind("<Button-1>", self.callback)

然后你就可以从事件中获取按钮文本,你不需要知道它的行/列位置,只需按下它:

then you can get the button text from the event, you do not need to know its row/col position, only to press it:

def callback(self, event):
    mybutton = event.widget
    text_at_row_col = mybutton["text"]

这篇关于在网格上查找小部件(tkinter 模块)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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