sch*_*be5 5 python text tkinter widget
我使用了一个 Text 小部件,Tkinter当按下按钮时,它会将选定的文本复制到剪贴板。现在在复制过程之后,我想取消选择文本。知道这可能如何工作吗?
由于似乎有必要发布一些代码,以便人们更详细地了解我的问题,这里是:
def copy_to_clipboard(self, copy_string):
#copy to clipboard function
self.clipboard_clear()
try:
#text in outputlistfield marked, so copy that
self.clipboard_append(self.outputlistfield.get("sel.first", "sel.last"))
except:
#no text marked
Run Code Online (Sandbox Code Playgroud)
outputlistfield是一个文本小部件。如果选择文本,则应将其复制到剪贴板。这很好用。但我想重置选择,以便在复制文本后不再选择文本。那么,有什么建议吗?
self.outputlistfield.tag_remove(SEL, "1.0", END)就可以了。如本例所示:
from tkinter import ttk
from tkinter import *
class Main(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
self.title("Test")
self.text = Text(self)
self.text.insert(END, "This is long long text, suitable for selection and copy.")
self.text.pack(expand=True, fill=BOTH)
self.frame = ttk.Frame(self)
self.frame.pack(expand=True, fill=BOTH)
self.button1 = ttk.Button(self.frame, text="Copy", command=self.OnCopy)
self.button1.pack(expand=True, fill=BOTH)
self.button2 = ttk.Button(self.frame, text="Copy & Unselect", command=self.OnCopyDeselect)
self.button2.pack(expand=True, fill=BOTH)
def OnCopy(self):
try:
text = self.text.selection_get()
except TclError:
print("Select something")
else:
self.clipboard_clear()
self.clipboard_append(text)
self.text.focus()
def OnCopyDeselect(self):
self.OnCopy()
self.text.tag_remove(SEL, "1.0", END)
root = Main()
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1929 次 |
| 最近记录: |