Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 修改后的tkinter回调_Python 3.x_Tkinter - Fatal编程技术网

Python 3.x 修改后的tkinter回调

Python 3.x 修改后的tkinter回调,python-3.x,tkinter,Python 3.x,Tkinter,我将一个事件绑定到一个文本小部件,以便跟踪其文本中的所有更改。在将新字符添加到小部件文本之前调用此事件。我需要的是在添加新角色后调用的事件,或类似的事件。这里有一个小的更改,我相信它可以满足您的需要: class ModifiedMixin: ''' Class to allow a Tkinter Text widget to notice when it's modified. To use this mixin, subclass from Tkinter.Tex

我将一个事件绑定到一个文本小部件,以便跟踪其文本中的所有更改。在将新字符添加到小部件文本之前调用此事件。我需要的是在添加新角色后调用的事件,或类似的事件。

这里有一个小的更改,我相信它可以满足您的需要:

class ModifiedMixin:
    '''
    Class to allow a Tkinter Text widget to notice when it's modified.

    To use this mixin, subclass from Tkinter.Text and the mixin, then write
    an __init__() method for the new class that calls _init().

    Then override the beenModified() method to implement the behavior that
    you want to happen when the Text is modified.
    '''

    def _init(self):
        '''
        Prepare the Text for modification notification.
        '''

        # Clear the modified flag, as a side effect this also gives the
        # instance a _resetting_modified_flag attribute.
        self.clearModifiedFlag()

        # Bind the <<Modified>> virtual event to the internal callback.
        self.bind_all('<<Modified>>', self._beenModified)

    def _beenModified(self, event=None):
        '''
        Call the user callback. Clear the Tk 'modified' variable of the Text.
        '''

        # If this is being called recursively as a result of the call to
        # clearModifiedFlag() immediately below, then we do nothing.
        if self._resetting_modified_flag: return

        # Clear the Tk 'modified' variable.
        self.clearModifiedFlag()

        # Call the user-defined callback.
        self.beenModified(event)

    def beenModified(self, event=None):
        '''
        Override this method in your class to do what you want when the Text
        is modified.
        '''
        pass

    def clearModifiedFlag(self):
        '''
        Clear the Tk 'modified' variable of the Text.

        Uses the _resetting_modified_flag attribute as a sentinel against
        triggering _beenModified() recursively when setting 'modified' to 0.
        '''

        # Set the sentinel.
        self._resetting_modified_flag = True

        try:

            # Set 'modified' to 0.  This will also trigger the <<Modified>>
            # virtual event which is why we need the sentinel.
            self.tk.call(self._w, 'edit', 'modified', 0)

        finally:
            # Clean the sentinel.
            self._resetting_modified_flag = False


if __name__ == '__main__':
    from Tkinter import Text, BOTH, END

    class T(ModifiedMixin, Text):
        '''
        Subclass both ModifiedMixin and Tkinter.Text.
        '''

        def __init__(self, *a, **b):

            # Create self as a Text.
            Text.__init__(self, *a, **b)

            # Initialize the ModifiedMixin.
            self._init()

        def beenModified(self, event=None):
            '''
            Override this method do do work when the Text is modified.
            '''
            print('Hi there.', self.get(1.0, END))

    t = T()
    t.pack(expand=1, fill=BOTH)
    t.mainloop()
类修改器dmixin:
'''
类以允许Tkinter文本小部件在修改时注意到它。
要使用此mixin,请从Tkinter.Text和mixin中创建子类,然后编写
调用_init()的新类的uuu init_uu()方法。
然后重写beenModified()方法以实现
您希望在修改文本时发生这种情况。
'''
定义初始化(自):
'''
准备修改通知的文本。
'''
#清除“修改”标志,作为一种副作用,这也会使
#实例a\u重置\u修改的\u标志属性。
self.clearModifiedFlag()
#将虚拟事件绑定到内部回调。
self.bind\u all(“”,self.\u已修改)
已修改定义(自,事件=无):
'''
调用用户回调。清除文本中的Tk“modified”变量。
'''
#如果这是由于调用
#在下面的clearModifiedFlag()中,我们什么也不做。
如果是自复位修改标志:返回
#清除Tk“modified”变量。
self.clearModifiedFlag()
#调用用户定义的回调函数。
自我修改(事件)
def已修改(自身,事件=无):
'''
在类中重写此方法,以便在文本
是修改过的。
'''
通过
def clearModifiedFlag(自):
'''
清除文本中的Tk“modified”变量。
使用_restating_modified_flag属性作为针对
将“modified”设置为0时递归触发_beenModified()。
'''
#设置哨兵。
self.\u重置\u修改\u标志=真
尝试:
#将“已修改”设置为0。这也将触发
#虚拟事件,这就是为什么我们需要哨兵。
self.tk.call(self._w,'edit','modified',0)
最后:
#清理哨兵。
self.\u重置\u修改\u标志=False
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
从Tkinter导入文本,两个,结束
T类(ModifiedMixin,文本):
'''
子类ModifiedMixin和Tkinter.Text。
'''
定义初始化(self,*a,**b):
#将self创建为文本。
文本.uuuu初始化(self,*a,**b)
#初始化ModifiedMixin。
self._init()
def已修改(自身,事件=无):
'''
重写此方法在修改文本时不起作用。
'''
打印(“你好”,self.get(1.0,结束))
t=t()
t、 包装(展开=1,填充=2)
t、 mainloop()

我保留了整个结构,包括评论,完好无损;更改是打印,使其成为一项功能,并显示小部件的当前内容,以确认它们是修改后的内容,如您所需。

oooo-kay。不了解一切;但这似乎扼杀了特金特的事件。我需要一个事件在修改前和修改后。另外,如果这只适用于文本窗口小部件,那么为所有窗口小部件添加一个after configure事件将是一件痛苦的事情。从技术上讲,这不是一个重复的问题,因为它是先问的,但另一个问题得到了更好的答案,可能是因为它在编写时正确地标记了“tkinter”。