2010-08-09 29 views
0

我已經爲了遵循其文本的所有更改綁定一個事件到一個文本控件。在將新角色添加到小部件的文本之前調用此事件。我需要的是在添加新角色之後調用的事件,或類似的東西。Tkinter的回調之後修改

+0

的可能重複[如何綁定在Tkinter的文本小自事件發生後,將通過文本控件綁定?](http://stackoverflow.com/questions/3501849/how-to-bind-self-events-in- Tkinter的文本的小部件 - 後它,將-綁定按文本widg)。從技術上講,這不是一個重複,因爲它是第一個要求,但其他問題得到更好的答案,可能是因爲它被正確標記爲「Tkinter的」這是寫的時候。 – 2014-01-03 18:27:00

回答

0

這裏有一個Cookbook recipe,我相信小改你想要做什麼:

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() 

我已經離開了整體結構,包含註釋,完整的;變化是對print,使之成爲功能並顯示窗口小部件的當前內容,以確認他們的內容後的修改,根據您的需要。

+0

oooo-kay。不明白一切;但這似乎殺死了tkinter的事件。我需要修改前的事件和修改後的事件。另外,如果這隻適用於Text小部件,那麼爲所有小部件添加一個after-configure事件將是一件痛苦的事情。 – Rawing 2010-08-10 16:07:26