from tkinter import StringVar
class StringVar_WithHistory(StringVar):
def __init__(self):
self.history = []
def set (self,value):
if StringVar.get(self) != value:
StringVar.set(self, value)
self.history.append(value)
def undo (self):
StringVar.set(self,history[-1])
self.history.pop()
from tkinter import OptionMenu
class OptionMenuUndo(OptionMenu):
def __init__(self,parent,title,*option_tuple,**configs):
self.result = StringVar_WithHistory()
self.result.set(title)
OptionMenu.__init__(self,parent,self.result,*option_tuple,**configs)
def get(self):
return self.result.get()
def undo(self):
self.result.undo()
def simulate_selection(self,option):
self.result.set(option)
我正在处理StringVar_with history类,使其应用于optionmenundo类。StringVar_with history有三种方法。在
init(self):初始化基类;创建一个历史记录列表,用于存储调用的值集。在
set(self,value):如果值与当前值不同,则将StringVar设置为value并在历史记录列表中记住它(如果它与当前值相同,则不执行任何操作:无新选择)。在
undo(self):通过更新StringVar和历史列表来撤消最近选择的选项
我试图运行代码,但出现以下错误:
^{pr2}$
有人能告诉我怎样用历史来修复我的心灵吗?谢谢