4

How can I create a text field in Tkinter where my mouse pointer becomes active to select and copy that text content, like in the following screenshot?

Example

It is a Windows media player song properties window.

wovano
  • 4,543
  • 5
  • 22
  • 49
mahes
  • 1,074
  • 3
  • 12
  • 20

1 Answers1

5

You can set the state of Text to disabled:

root = tkinter.Tk()
input = tkinter.Text(root)
input.configure(state=tkinter.DISABLED)

And to enable it again:

input.configure(state=tkinter.NORMAL)

You could also set the styling, to make it look more like your screenshot:

input.configure(state=tkinter.DISABLED,
    borderwidth=0,
    background=root.cget('background'))

And to add text:

input.insert(tkinter.END, "I'd like to suggest that coconuts migrate.")

Also see: Methods on Text widgets.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
  • 1
    @user2332665 Use the `insert()` method; I've updated my answer with an example (this info was also in the link I posted, by the way). – Martin Tournoij Feb 23 '14 at 08:19
  • 1
    @user2332665 Right. You usually want `tk.END`, which means "append to end". But you do a lot more, [here's a full description](http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/text-index.html) if the index parameter. – Martin Tournoij Feb 23 '14 at 08:23
  • How should I update the content in the Text widget. For eg, I have an Entry widget that takes an integer and a Button upon pressing will display multiplication table of that integer on the Text field(only multiplication table of current integer). Should I delete the Text widget and create a fresh one with new data. – mahes Feb 23 '14 at 08:46
  • You should be able to use `input.delete(0, tkinter.END)` to remove the contents. (not tested, [lifted from this question](http://stackoverflow.com/questions/2260235/how-to-clear-the-entry-widget-in-a-gui-after-a-button-is-pressed-in-python/2262084#2262084)) – Martin Tournoij Feb 23 '14 at 09:05
  • once we use `input.configure(state=tkinter.DISABLED)` the widget is disabled for ever, we can't use `input.delete(0, tkinter.END)`, or any further updation on it. what I did for updating is creating the same widget(fresh) again.. – mahes Feb 24 '14 at 15:16