找到你要的答案

Q:How do you find a unique and constant ID of a widget?

Q:你如何找到一个独特的和恒定的ID的小部件?

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 :)

注意,通过小部件这不包括帆布项目(这不是部件)。

我的目标是建立两个类:一个生产项目的帆布部件,其他生产小工具自己。这是为了确保我可以移动周围的东西在窗户上,让它在重新开放。我已经这样做帆布项目。你看,画布部件实际上返回一个实际的身份,仍然是受人尊敬的项目不断,因此我可以引用它的坐标。

然而,对于widgets,似乎没有这样的方式获得一个ID,我试过的部件。getint(),部件。getvar(),等我也尝试repr(插件)和身份(Widget),但这些价值观的变化后,重新开放,我认为新创建小部件,其中变量指的是一个新创建的控件到一只破坏比较,即使有相同的性质。

我也试着在父窗口,如窗口或框架中放置这些小部件,但是这些小部件本身并没有给它们的子部件分配任何唯一的值。

因此,基本上,我可以获得任何给定的部件的唯一值,但没有一个是恒定的。这可以由任何价值,因为我把它给一个字符串,将它添加到字典,并指定坐标STR参考具体部件说。

提前感谢您的任何指导,不胜感激:

answer1: 回答1:

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,但你可以给小部件一个独特的,恒定的ID。

引擎盖下,Tkinter使用TK。在TK,每一个部件都有一个名字,并通过点符号表示一个层次的生活。在TK你必须定义一个名称创建一个控件时;这是它为你做的。当你看到一个Widget的名字一样”。1234567890.0987654f321”,这意味着父母的“内部名称。1234567890”和“0987654321”的部件。

使用名称参数创建一个小部件时,可以通过给出名称来重写自动生成的名称。然后你可以使用STR把名字。

例如:

>>> 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>
python  user-interface  python-3.x  tkinter  widget