1

在 wxPython 应用程序中创建的每个对象都会创建一个id. 它可以作为参数给出,也可以使用id=wx.NewId()自动创建。

据我了解,使用一个对象,id您可以从其他地方引用该对象,但我找不到任何关于如何完成的简单解释。

谁能指出我正确的方向或对此有所了解?

(注意:我不希望通过 ID 绑定事件,这是我在各处找到的唯一教程。)

4

2 回答 2

4

免责声明:我从 OPs 问题中提取了这个答案。答案不应包含在问题本身中。


Jase提供的答案,但基于Joran Beasley的答案:

FindWindowById()在 class 中找到的函数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对象的 an,可以获得指向该对象的指针,以便进一步处理它。

于 2018-06-25T21:03:56.650 回答
2

我不认为有这样做的内置方式......但你可以做这样的事情

my_ids = {}

def widget_factory(widget_class,parent,id,*args,**kwargs):
     w = widget_class(parent,id,*args,**kwargs)
     my_ids[id] = w

def get_widget_by_id(widget_id):
     return my_ids[widget_id]

显然有一个功能......

http://wxpython.org/docs/api/wx.Window-class.html#FindWindowById

于 2012-09-21T20:03:49.740 回答