-3

Is it utterly impossible to receive a list from user input in Tkinter? Something along the lines of an HTML textarea box - where a user can (1) copy and paste a list of things into a text box, and then (2) tkinter translates the input list into a list of strings, then (3) can assign them to a value and do fun python stuff etc

I have reasonable faith I can accomplish parts (2) and (3), but I'm stuck on (1).

I have explored Entry, which basically accomplishes that but awkwardly and with poor visibility onto the pasted items in the tiny Entry box. I have explored Listbox, which doesn't allow user input in the way of generating a new list from nothing?

The running example is: if I want to input some groceries into a variable, I can copy-paste a text list and paste as one item (rather than separately) --

eg: ["apples", "oranges", "raspberries"] clicks submit VS ["apples"] clicks submit ["oranges"] clicks submit ["raspberries"] clicks submit

-- Anyone have any recommendations for that elusive textarea-like input box for tkinter? Do I just wrestle with the Entry tiny box?

Ksofiac
  • 382
  • 1
  • 6
  • 21

1 Answers1

3

You want a tkinter.Text

import tkinter as tk

# proof of concept
root = tk.Tk()
textarea = tk.Text(root)
textarea.pack()
root.mainloop()

You can retrieve the text with textarea.get in the normal way

result = textarea.get(1.0, 'end')     # get everything
result = textarea.get(1.0, 'end-1c')  # get exactly what the user entered
                                      # (minus the trailing newline)
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
  • 3
    You might want to also mention that if you split on newlines, the data will be a list. Also, you normally want to used `end-1c` rather than `end`, since the latter will pick up an extra newline that tkinter adds automatically, `end-1c` is the canonical way to get exactly what the user entered. – Bryan Oakley Nov 20 '17 at 22:09
  • @BryanOakley OP seemed confident he could manipulate the text to his desired result once he knew how to build that result, so I'm going to leave the `splitlines` out, however I included your comment there (I actually didn't know that, I've always rstripped after getting!) – Adam Smith Nov 20 '17 at 22:39