python - wxpython 从 ID 访问对象

标签 python wxpython python-2.7

在 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/

相关文章:

python - 如何使用 pandas groupby 对某些行进行降序排序,对某些行进行升序排序

python - wxpython:EVT_GRID_CELL_CHANGED 问题

python - 如何使用 python 从给定文件中提取行 block

python - 问题读取 csv 文件

python - 如何在 SQLAlchemy 中使用来自 backref(.., order_by=..) 内部混合类的列名?

python - 用查询结果填充字典

python - 为什么我们需要 asyncio 同步原语,何时使用它们?

python - Flask 添加参数以查看 before_request 中的方法

python - 创建后调整 wx.ListCtrl 的大小

python - 单纯使用线程来更新 GUI 还不够吗?