简体   繁体   English

python / tkinter:是否可以复制粘贴文本和标签?

[英]python/tkinter: is it possible to copy paste text AND tags?

in my Text widget I have here and there many tags, taking care of the text appearance (bold, italic etc). 在我的文本小部件中,我有很多标签,处理文本外观(粗体,斜体等)。

Tkinter takes care by default of cut/copy/paste in the text widget, but only of the text content, not of the associated tags. Tkinter默认在文本小部件中剪切/复制/粘贴,但仅限于文本内容,而不是关联标签。 Do you know if it is possible to implement it? 你知道是否有可能实现它?

I anticipate some problems if I would cut in the middle a region where eg bold text appears: I would have to leave behind a tag marker for 'bold begin' so that the remaining text is still bold... 我预计会遇到一些问题,如果我在中间切出一个例如粗体文本的区域:我将不得不留下一个标记标记为“粗体开始”,以便剩下的文本仍然是粗体...

Yes, it's possible to implement it. 是的,可以实现它。 You can use the dump method of the text widget to get all the text and tags in a given range. 您可以使用文本小部件的dump方法获取给定范围内的所有文本和标记。 To paste, you just have to use that information when inserting the text. 要粘贴,您只需在插入文本时使用该信息。

It gets somewhat complicated when you try to paste, say, italic text inside a range of already bolded text since you may have to retag the inserted text to have both bold and italic attributes. 当您尝试在一系列已经加粗的文本中粘贴斜体文本时,它会变得有些复杂,因为您可能必须重新插入插入的文本以具有粗体和斜体属性。 However, I know it can be done because I've done it with tcl/tk in the past. 但是,我知道可以这样做,因为我以前用tcl / tk完成了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM