简体   繁体   English

Tkinter事件/几何传播

[英]Tkinter Event/Geometry Propagation

I am currently building my own text editor in Tkinter, and I am struggling to understand what event and geometry propagation is and how it works. 我目前正在Tkinter中构建自己的文本编辑器,并且正在努力了解什么是事件和几何图形传播以及它是如何工作的。 I have dug all over the internet to find a definite answer, but I have yet to find one. 我已经在互联网上挖了一个确定的答案,但还没有找到答案。

With that said, can someone please give me the rundown of what propagation is for events and the geometry manager? 话虽如此,请问有人可以概述事件和几何图形管理器的传播方式吗? I feel like having a post focused on propagation will be beneficial to me and many other Tk/Tkinter users. 我觉得发布一个专注于传播的帖子对我和其他许多Tk / Tkinter用户都是有益的。

Here are some specific examples of what confuses me: 以下是一些使我感到困惑的具体示例:

This method autocompletes curly braces, but why must I return "break" to have this work properly? 此方法会自动完成花括号,但是为什么我必须返回“ break”才能使其正常工作? Some other StackOverflow post (can't find it anymore) said that it stops the event from further propagating, but I do not know what this means. 其他一些StackOverflow帖子(无法再找到它)说,它阻止了事件的进一步传播,但是我不知道这意味着什么。

def _curly_pressed(self, event):
    self.text_field.insert(INSERT, "{}")
    self.text_field.mark_set(INSERT, f"{self.text_field.index(INSERT).split('.')[0]}.{int(self.text_field.index(INSERT).split('.')[1]) - 1}")
    return "break"

Furthermore, I wanted to keep the window from resizing when the Text widget fills with text. 此外,当“文本”窗口小部件填充文本时,我想防止窗口大小调整。 To do this, I had to use the grid_propogate method on the Frame containing the Text widget. 为此,我必须在包含Text小部件的Frame上使用grid_propogate方法。 I do not understand this, though. 我不明白这一点。

self.grid_propagate(False)

An event can be bound to a widget, such as an instance of Text, to a widget class, such as Text, or all widgets. 可以将事件绑定到诸如Text的实例的窗口小部件,诸如Text的窗口小部件类或所有窗口小部件。 For the first, this reference says 首先, 该参考资料

w.bind(sequence=None, func=None, add=None) w.bind(sequence = None,func = None,add = None)

This method is used to attach an event binding to a widget. 此方法用于将事件绑定附加到窗口小部件。 See Section 54, “Events” for the overview of event bindings. 有关事件绑定的概述,请参见第54节“事件”。

The sequence argument describes what event we expect, and the func argument is a function to be called when that event happens to the widget. 序列参数描述了我们期望的事件,而func参数是该事件发生在窗口小部件上时要调用的函数。 If there was already a binding for that event for this widget, normally the old callback is replaced with func, but you can preserve both callbacks by passing add='+'. 如果此窗口小部件已存在该事件的绑定,通常将旧的回调函数替换为func,但您可以通过传递add ='+'保留两个回调函数。

If you bound two events to a widget, the first event handler could specify whether it handled the event or not by returning 'break' or None. 如果将两个事件绑定到窗口小部件,则第一个事件处理程序可以通过返回'break'或None来指定是否处理该事件。

Binding an event to a widget does not replace class and all bindings. 绑定事件到窗口小部件替代类和所有绑定。 The Text class has an event handler for all visible key events, which inserts the character into the Text instance. Text类具有用于所有可见按键事件的事件处理程序,该事件处理程序将字符插入到Text实例中。 You did not say what did not work properly, but I presume that without 'break', the Text key event handler inserted another '{' character. 您没有说什么不能正常工作,但是我想如果没有'break',Text键事件处理程序会插入另一个'{'字符。 Since your handler already does that, you want to prevent the second insertion with 'break'. 由于您的处理程序已经执行过此操作,因此您想使用“ break”防止第二次插入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM