英文:
Is there are a Python Tkinter method in the Text widget for joining the Undo/Redo stack?
问题
I am seeking a method that can have the functionality of joining the undo/redo stack of the Tkinter Text widget written in the Python language. Is there something like the method to merging the user input undo/redo stack programmatically, or merging the programmatic changes to the Tkinter Text widget programmatically?
For example, I want to have the following:
import tkinter as tk
root = tk.Tk()
root.title("Stackoverflow question")
text = tk.Text(root)
text.some_method()
text.insert(tk.END, "a")
text.insert(tk.END, "b")
text.insert(tk.END, "c")
text.reversing_method()
text.pack()
root.mainloop()
As you can see, after calling some_method
, the user will have undone the whole string "abc" (and of course, after redo the steps, the string "abc" will be back again in pairs but not in sequence of "a", "b", and "c") after the text is entered programmatically (and by the user). However, I would like to seek a method also, when the reversing_method
is called, the user can undo the text entered programmatically or by them single-by-single.
英文:
I am seeking a method that can have the functionality of joining the undo/redo stack of the Tkinter Text widget written in the Python language. Is there something like the method to merging the user input undo/redo stack programmatically, or merging the programmatic changes to the Tkinter Text widget programmatically?
For example, I want to have the following:
import tkinter as tk
root = tk.Tk()
root.title("Stackoverflow question")
text = tk.Text(root)
text.some_method()
text.insert(tk.END, "a")
text.insert(tk.END, "b")
text.insert(tk.END, "c")
text.reversing_method()
text.pack()
root.mainloop()
As you can see, after calling some_method
, the user will have undone the whole string "abc" (and of course, after redo the steps, the string "abc" will be back again in pairs but not in sequence of "a", "b", and "c") after the text is entered programmatically (and by user). However, I would like to seek a method also, when the reversing_method
is called, the user can undo the text entered programmatically or by them single-by-single.
答案1
得分: 2
你可以。
如果你正在使用Tkinter版本 >= 8.5
(就我所知,我使用的是Tkinter 8.6,你的操作系统可能不同;不过,我猜以下方法与Tkinter版本 <= 8.4 兼容)
- 将你的
some_method
替换为text.config(autoseparators=False)
;以及 - 将你的
reverse_method
替换为text.config(autoseparators=True)
。
附言:如果你想要以编程方式 和 手动添加撤销/重做堆栈,你可以使用text.edit_separator()
方法。
MRE(最小可复现示例):
import tkinter as tk
root = tk.Tk()
root.title("StackOverflow答案来自Misinahaiya")
text = tk.Text()
text.config(autoseparators=False) # 或者根据你的喜好使用.configure
#...
text.config(autoseparators=True) # 默认值,作为反转方法
root.mainloop()
英文:
You may.
If you are using Tkinter versioned >= 8.5
(so far I have known as I'm using Tkinter version 8.6 and your operating system may be vary; however, I guess the following methods are compatible with Tkinter versioned <= 8.4)
- Replace your
some_method
withtext.config(autoseparators=False)
; and - Replace your
reverse_method
withtext.config(autoseparators=True)
.
P.S. If you want to add the undo/redo stack programmatically and manually, you may use the method text.edit_separator()
.
MRE:
import tkinter as tk
root = tk.Tk()
root.title("StackOverflow answer from Misinahaiya")
text = tk.Text()
text.config(autoseparators=False) # or .configure as you like
#...
text.config(autoseparators=True) # the default, as the reversing method
root.mainloop()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论