python tkinter popup window with selectable text

夙愿已清 提交于 2019-12-07 09:01:20

问题


I want to make popup window using Tkinter. I can do it so:

import Tkinter
a="some data that use should be able to copy-paste"
tkMessageBox.showwarning("done","message")

But there is one problem that user need to be able to select, copy and paste shown text. It's not possible to do in such way.

Are there any ways to do it with Tkinter? (or another tools that is supplied with python by default)

Thanks in advance for any tips


回答1:


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 :)




回答2:


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)


来源:https://stackoverflow.com/questions/13519192/python-tkinter-popup-window-with-selectable-text

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