Tebe
Tebe

Reputation: 3214

python tkinter popup window with selectable text

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

Upvotes: 4

Views: 5766

Answers (2)

bernard paulus
bernard paulus

Reputation: 1664

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

Upvotes: 3

Niels
Niels

Reputation: 482

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)

Upvotes: 0

Related Questions