以下のコードは、削除可能なテスト関数(forceChange)を含んでいますが、残りのコードはPythonのリスト変数をトレースしています。すでにtkイベントループを使用しているので、私はそれを使用しましたが、GUIがない場合にイベントをスケジュールするためにschedとtimeモジュールを使ってテストしました。
from tkinter import * import sys
class ListManager:
def __init__(self, root, listvar):
self.root = root
self.listvar = listvar
self.previous_value = listvar.copy()
# Create an event to watch for changes to the list.
self.watch_event = root.after(20, self.watchList)
# Create an event to change the list. This is for test purposes only.
self.change_event = root.after(200, self.forceChange)
def watchList(self):
''' Compare the previous list to the current list.
If they differ, print a message (or do something else).
'''
try:
if self.previous_value != self.listvar:
print("Changed! Was:", self.previous_value, " Is:", self.listvar)
self.previous_value = self.listvar.copy()
# Reschedule this function to continue to check.
self.root.after(20, self.watchList)
except Exception:
print("Variable has been destroyed")
self.root.after_cancel(self.change_event)
def forceChange(self):
try:
next = self.listvar[-1:][0]
except:
# Variable was destroyed.
return
if next == 11:
sys.exit()
next += 1
self.listvar.append(next)
self.root.after(500, self.forceChange)
if __name__ == '__main__':
root = Tk()
# This is the list we'll watch.
mylist = [1, 2, 3]
# Create a list manager
vlist = ListManager(root, mylist)
root.mainloop()
多分リストのラッパーはあなたが後にしているものです:[リンク](http://stackoverflow.com/questions/37799938/what-happens-when-we-editappend-remove-a-list -and-can-we-execute-actions-e)を実行します。私はまだあなたがいくつか(重い)持ち上げる必要があると思う.... – arrethra