在 wxPython 应用程序中创建的每个对象都会创建一个 id
。它可以作为参数给出,也可以使用 id=wx.NewId()
自动创建。
据我了解,使用对象的 id
,您可以从其他地方引用该对象,但我找不到关于如何完成的任何简单解释。
任何人都可以指出我正确的方向或者对此有所启发吗?
(注意:我不想通过 ID 绑定(bind)事件,这是我在各处找到的唯一教程。)
最佳答案
免责声明:我从 OP 问题中提取了这个答案。 Answers should not be contained in the question itself .
答案由 Jase 提供, 但基于 answer by Joran Beasley :
函数 FindWindowById()
在类 wx.Window
中找到,大多数小部件都是它的子类。
通过在父对象上调用此函数(还没有尝试过祖父对象等),它会返回相关对象的指针(副本?),以便:(在交互式解释器中)
import wx
app = wx.App()
frame = wx.Frame(None)
but = wx.Button(frame, -1, label='TestButton')
frame2 = wx.Frame(None)
butId = but.GetId()
test = wx.Window.FindWindowById(butId) # Fails with TypeError
# TypeError: unbound method FindWindowById() must be called with Window instance as
# first argument (got int instance instead)
test = Frame2.FindWindowById(butId) # returned either a None object or nothing at all.
test = Frame.FindWindowById(butId) # returned a pionter (copy?) of the object in such a
# manner that the following worked:
label = test.GetLabel()
print label # displayed u'TestButton'
因此,通过了解对象的 id
,可以获得指向该对象的指针,以便进一步处理它。
关于python - wxpython 从 ID 访问对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12537296/