如何在事件处理程序中访问外部对象?
正如标题所示,我正在 Tkinter 中的运动触发事件处理程序中抓取光标位置。
我想用位置更新现有的标签小部件。但是,我一生都无法弄清楚如何在事件处理程序中编辑 Label
文本字段(或任何与此相关的外部对象)。据我了解,事件是传递给处理程序的唯一参数,这意味着我无法传递标签对象。
如何访问处理程序之外的对象?
As the title says, I'm grabbing the cursor location within a motion triggered event handler in Tkinter.
I'd like to update an existing label widget with the location. However, I cannot for the life of me figure out how to edit the Label
text field (or any external object for that matter) within the event handler. From what I understand, event is the only argument passed to the handler, which means I can't pass the label object.
How do I access objects outside of the handler?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Tkinter 不会在事件处理程序中传递对象,无论如何它如何知道您对哪个对象感兴趣?
相反,您有责任从事件处理程序访问您希望更新的对象,例如,您的事件处理程序可以是简单的函数,它可以访问全局对象,也可以是对象的方法,并且可以通过 self 访问该对象。
这是一种使用全局对象的方法
同样的事情可以以面向对象的方式完成
Tkinter won't pass around objects in event handler, and anyway how it would know in which object you are interested in?
Instead it is your responsibility to access the objects you wish to update from event handler, e.g. your event handler could be simple function and it could access global object, or it can be a method of an object and can access that object via self.
Here is a way using global objects
Same thing can be done in an object oriented way