我已經爲了遵循其文本的所有更改綁定一個事件到一個文本控件。在將新角色添加到小部件的文本之前調用此事件。我需要的是在添加新角色之後調用的事件,或類似的東西。Tkinter的回調之後修改
0
A
回答
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的
相關問題
- 1. Python tkinter文本修改回調
- 2. 如何修改Tkinter Entry小部件時獲得事件回調?
- 3. tkinter:檢查修改
- 4. 返回修改後的DataSet
- 5. Tkinter回調類
- 6. Tkinter回調
- 7. 修改後回不工作
- 8. Python的 - 從Tkinter的回調
- 9. Tkinter中的定時回調?
- 10. 使用C++的文件修改回調?
- 11. 不能修改PARAMS在EnumWindows的回調
- 12. 任何綁定回調之前修改Ajax響應
- 13. 在成功回調之前修改JSONP結果
- 14. 修改Python中的默認字體Tkinter
- 15. Radibuttons正在修改錯誤的值tkinter
- 16. 如何在git checkout和git pull之後找回修改後的文件?
- 17. Javascript:在調用它們之前修改回調函數的參數
- 18. FragmentTransaction setCustomAnimation回調之前和之後
- 19. coldfusion cfinclude回調之前和之後
- 20. Tkinter字符串回調
- 21. Python tkinter RadioButton回調函數
- 22. Python Tkinter按鈕回調
- 23. 用jQuery修改後寫回作用域
- 24. 修改子記錄在before_save回調
- 25. 使用RxAndroid MVP Retrofit修改回調
- 26. 修改jQuery jsonp回調函數?
- 27. 手機聯繫人修改回調
- 28. 從回調中修改jQuery ajax設置
- 29. Activerecord回調修改相關模型
- 30. Tkinter命令Lambda函數修改變量
的可能重複[如何綁定在Tkinter的文本小自事件發生後,將通過文本控件綁定?](http://stackoverflow.com/questions/3501849/how-to-bind-self-events-in- Tkinter的文本的小部件 - 後它,將-綁定按文本widg)。從技術上講,這不是一個重複,因爲它是第一個要求,但其他問題得到更好的答案,可能是因爲它被正確標記爲「Tkinter的」這是寫的時候。 – 2014-01-03 18:27:00