在Text小部件中,有一个Python Tkinter方法可以用于合并撤销/重做堆栈吗?

huangapple go评论51阅读模式
英文:

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 &gt;= 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 with text.config(autoseparators=False); and
  • Replace your reverse_method with text.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(&quot;StackOverflow answer from Misinahaiya&quot;)

text = tk.Text()
text.config(autoseparators=False) # or .configure as you like
#...
text.config(autoseparators=True) # the default, as the reversing method

root.mainloop()

huangapple
  • 本文由 发表于 2023年3月31日 17:19:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定