tkinter 撤销操作实现代码
时间: 2023-02-12 18:21:27 浏览: 141
下面是一个简单的 tkinter 撤销操作实现代码示例:
```
import tkinter as tk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("Undo Example")
self.geometry("200x200")
self.undo_list = []
self.label = tk.Label(self, text="Label Text")
self.label.pack()
self.entry = tk.Entry(self)
self.entry.pack()
self.button = tk.Button(self, text="Change Label", command=self.change_label)
self.button.pack()
self.undo_button = tk.Button(self, text="Undo", command=self.undo)
self.undo_button.pack()
def change_label(self):
self.undo_list.append(self.label["text"])
self.label["text"] = self.entry.get()
def undo(self):
if self.undo_list:
self.label["text"] = self.undo_list.pop()
app = App()
app.mainloop()
```
这个示例中,我们使用了一个 `undo_list` 列表来存储每次修改的标签文本。当按钮“Change Label”被点击时,我们将当前标签文本添加到列表中,然后将标签文本更改为输入框中的文本。当按钮“Undo”被点击时,我们从列表中弹出最后一个元素并将其作为标签文本。