Anem
Anem

Reputation: 7

in tkinter how do I access text in a text box as a variable

What I am trying to achieve is being able to click a button and get the text that the user has typed into a variable.

I already have a button and the text box

from tkinter import *

def Enter_button():
    print()

ws = Tk()
ws.title('quick look up')
ws.geometry('300x300')
ws.config(bg='#ffffff')

message ='''Data here:'''


text_box = Text(
    ws,
    height=13,
    width=40
)
text_box.pack(expand=True)
text_box.insert('end', message)

Button(
    ws,
    text='Submit',
    width=15,
    height=2,
    command=Enter_button
).pack(expand=True)

ws.mainloop()

Upvotes: 0

Views: 396

Answers (1)

Eli Harold
Eli Harold

Reputation: 2301

Somewhere in Enter_button you can add text_box.get("1.0",END).

get() takes the start index and ending index of the text you want to get. So for all the text use: .get("1.0",END) as shown above and below.

So, for example:

from tkinter import *

def Enter_button():
    print(text_box.get("1.0",END))

ws = Tk()
ws.title('quick look up')
ws.geometry('300x300')
ws.config(bg='#ffffff')

message ='''Data here:'''

text_box = Text(
    ws,
    height=13,
    width=40
)
text_box.pack(expand=True)
text_box.insert('end', message)

Button(
    ws,
    text='Submit',
    width=15,
    height=2,
    command=Enter_button
).pack(expand=True)

ws.mainloop()

Upvotes: 1

Related Questions