Python tkinter popup with selectable text

I want to create a popup using Tkinter. I can do it like this:

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

But there is one problem that the user should be able to select, copy and paste the displayed text. It is impossible to do so.

Are there any ways to do this with Tkinter? (or other tools that come with python by default)

Thanks in advance for any advice.

+4
source share
2 answers

From here , it seems that the 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 answer your comment, I found a way to insert multi-line text using the Text widget. Here is the draft decision:

 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
source

You can use the buttons to copy and paste. First you need to choose. In a text widget, this is easy to do with

 selection=nameoftextwidget.get(SEL_FIRST,SEL_LAST) 

You can then use this for easy copying using selection. If you want to copy / paste it into the same text widget, you can use:

 nameoftextwidget.insert(END,"\n"+selection) 
0
source

Source: https://habr.com/ru/post/1447811/


All Articles