简体   繁体   中英

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()

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()

If you want to be able to change it you have to make the variable with some text and then destroy and rebuild the label to update it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM