您如何找到小部件的唯一且恒定的 ID? [英] How do you find a unique and constant ID of a widget?

查看:23
本文介绍了您如何找到小部件的唯一且恒定的 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,通过小部件,这不包括画布项目(不是小部件).

我的目标是构建两个类:一个生成画布小部件的项目,另一个生成小部件本身.这是为了确保我可以在窗口中移动东西并在重新打开时将其保留在那里.我已经为画布项目做到了这一点.你看,画布小部件实际上返回一个实际的 ID,对于受尊重的项目来说,它保持不变,因此我可以参考它,它是坐标.

但是,对于widget本身来说,似乎没有这种获取ID的方式.我试过 widget.getint(), widget.getvar() 等.我也试过 repr(widget)id(widget),但是这两个值在重新打开时都会发生变化,我认为创建了新的小部件,其中变量仅指新创建的小部件与一个小部件相比刚刚销毁,即使它具有相同的属性.

我还尝试将所述小部件放在父窗口中,例如窗口或框架,但这些小部件本身不会为其各自的子小部件分配任何唯一值.

所以基本上,我可以获得任何给定小部件的唯一值,但没有一个是恒定的.顺便说一下,这可以是任何值,因为我将把它变成一个字符串,将它附加到一个字典,并将坐标分配给所述 str 以引用特定的小部件.

预先感谢您的任何指导,非常感谢:)

解决方案

你不能获得唯一的、恒定的 ID,但你可以小部件一个唯一的、固定 ID.

在幕后,tkinter 使用 tk.在 tk 中,每个小部件都有一个名称,并存在于通过点符号表示的层次结构中.在 tk 中,您必须在创建小部件时定义一个名称;这是 tkinter 为您做的事情.当您看到像.1234567890.0987654f321"这样的小部件名称时,这意味着父部件的内部名称为.1234567890",而小部件的名称为0987654321".

您可以通过在创建小部件时使用 name 参数提供名称来覆盖自动生成的名称.然后您可以使用 str 来获取名称.

例如:

<预><代码>>>>将 Tkinter 作为 tk 导入>>>根 = tk.Tk()>>>f = tk.Frame(root, name="foo")>>>b1 = tk.Button(f, name="b1")>>>字符串(b1)'.foo.b1'>>>root.nametowidget(".foo.b1")<Tkinter.Button 实例在 0x100795488>>>>b1<Tkinter.Button 实例在 0x100795488>

Note that by widget this excludes canvas items (which aren't widgets).

My goal is to build two classes: one that produces items of a canvas widget, the other producing widgets themselves. This is to ensure I can move stuff around in the window and keep it there upon reopening. I've already done this for canvas items. You see, canvas widgets actually return an actual ID that remains constant for the respected items, thus I can reference that and it's coords.

However, for widgets themselves, there seems to be no such way of obtaining an ID. I've tried widget.getint(), widget.getvar() , etc. I've also tried repr(widget) and id(widget), but both of these values change upon reopening, am thinking that new widgets are created, where the variable just refers to a newly created widget in comparison to the one just destroyed, even if it has identical properties.

I've also tried putting said widgets in a parent window, like window, or frame, but these widgets themselves do not assign any unique values to their respective child widgets.

So basically, I can obtain unique values of any given widget, but none that are constant. This could by any value by the way, for I shall just turn it to a string, append it to a dict, and assign the coords to said str to reference a specific widget.

Thank you in advance for any guidance, it is much appreciated :)

解决方案

You can't get a unique, constant ID, but you can give widgets a unique, constant ID.

Under the hood, tkinter uses tk. In tk, every widget has a name, and lives in a hierarchy expressed via dot notation. In tk you must define a name when creating a widget; this is something that tkinter does for you. When you see a widget name like ".1234567890.0987654f321", that means that the parent has the internal name of ".1234567890" and the widget is named "0987654321".

You can override the auto-generated name by giving a name when you create a widget, using the name parameter. You can then use str to get the name.

For example:

>>> import Tkinter as tk
>>> root = tk.Tk()
>>> f = tk.Frame(root, name="foo")
>>> b1 = tk.Button(f, name="b1")
>>> str(b1)
'.foo.b1'
>>> root.nametowidget(".foo.b1")
<Tkinter.Button instance at 0x100795488>
>>> b1
<Tkinter.Button instance at 0x100795488>

这篇关于您如何找到小部件的唯一且恒定的 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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