python tkinter popup window with selectable text

↘锁芯ラ 提交于 2019-12-05 12:40:50

From here, it seems a workaround using Entry in Tkinter is doable. Here is the code:

import Tkinter as Tk
root = Tk.Tk()

ent = Tk.Entry(root, state='readonly')
var = Tk.StringVar()
var.set('Some text')
ent.config(textvariable=var, relief='flat')
ent.pack()
root.mainloop()

EDIT: To respond to your comment, I found a way to insert multi-line text, using the Text widget. Here is a draft of a solution:

from Tkinter import *

root = Tk()
T = Text(root, height=2, width=30, bg='lightgrey', relief='flat')
T.insert(END, "Just a text Widget\nin two lines\n")
T.config(state=DISABLED) # forbid text edition
T.pack()
mainloop()

I'm (still) interested in any better solution :)

You can use buttons for copy and paste. First you need to select. In a text widget it is easily done by

selection=nameoftextwidget.get(SEL_FIRST,SEL_LAST)

Then you can use this for copying easily by the use of selection. If you want to copy/paste it in that same text widget, you can use:

nameoftextwidget.insert(END,"\n"+selection)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!