1

I'm trying to use tkinter.Text to create a text area in Python. With that, I want to get all the input they put into that text area and display it in the Entry field above it. It gives an error saying it needs two arguments.

from Tkinter import *

def create_index():
        var = body.get(0)
        link.insert(10,var)
        file.close()

master = Tk()
Label(master, text="Link:").grid(row=0)
Label(master, text="Body:").grid(row=1)

link = Entry(master)
body = Text(master)

link.grid(row=0, column=1)
body.grid(row=1, column=1)
Button(master, text='Show', command=create_index).grid(row=3, column=1, sticky=W, pady=4)

mainloop()
nbro
  • 15,395
  • 32
  • 113
  • 196
Jordan Little
  • 119
  • 1
  • 2
  • 8
  • 2
    That it **"needs to arguments"**? Please edit your question with the error it gives, that will be of great help. –  Jan 13 '15 at 00:51
  • Have you read the documentation for the text widget? The argument requirements are well documented. – Bryan Oakley Oct 19 '19 at 21:59

1 Answers1

7

To get all the input from a tkinter.Text, you should use the method get from the tkinter.Text object you are using to represent the text area. In your case, body should be the variable of type tkinter.Text, so here's an example:

text = body.get("1.0", "end-1c")  

tkinter.Text objects count their content as rows and columns. The "1.0" indicates exactly that: you want to get the content starting from line 1 and character 0 (this is the default starting point of a tkinter.Text object).

Here's a complete working example, where basically on the click of a button, the method get_text is called and adds the content of body to an tkinter.Entry object that I called entry (through the use of a variable of type tkinter.StringVar. See documentation for more information):

import tkinter

def get_text():
    content = body.get(1.0, "end-1c")
    entry_content.set(content)

master = tkinter.Tk()

body = tkinter.Text(master)
body.pack()

entry_content = tkinter.StringVar()
entry = tkinter.Entry(master, textvariable=entry_content)
entry.pack()

button = tkinter.Button(master, text="Get tkinter.Text content", command=get_text)
button.pack()

master.mainloop()

For another good example, see this other post and the first comment below.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
nbro
  • 15,395
  • 32
  • 113
  • 196
  • 3
    Note that by using `END` you're getting one more character than what the user entered -- you're getting the trailing newline that tkinter automatically adds. You might want to use `"end-1c"` to get all but the last newline, or trim the last newline. It's also worth noting that in your example the use of `StringVar` is completely unnecessary. You can directly set the contents of an entry widget without using an instance of `StringVar`. – Bryan Oakley Jan 13 '15 at 12:04
  • 1
    @BryanOakley Ok, thanks for the critics, I will edit at least the part of `END` ;) – nbro Jan 13 '15 at 12:07
  • @BryanOakley Since you are an expert in `tkinter` and `ttk` (from what I know), could you please also have a look to this answer I tried to give yesterday: http://stackoverflow.com/questions/27912250/how-to-set-the-background-color-of-a-ttk-combobox/27914316#27914316. I tried to solve the problem, but there are still some problems I describe in the answer. If you could try to improve it, it could also be useful for future visitors as a good reference (who knows?). Thanks :) – nbro Jan 13 '15 at 12:31