How to Get the Input from the Tkinter Text Widget

How to get the input from the Tkinter Text Widget?

To get Tkinter input from the text box, you must add a few more attributes to the normal .get() function. If we have a text box myText_Box, then this is the method for retrieving its input.

def retrieve_input():
input = self.myText_Box.get("1.0",END)

The first part, "1.0" means that the input should be read from line one, character zero (ie: the very first character). END is an imported constant which is set to the string "end". The END part means to read until the end of the text box is reached. The only issue with this is that it actually adds a newline to our input. So, in order to fix it we should change END to end-1c(Thanks Bryan Oakley) The -1c deletes 1 character, while -2c would mean delete two characters, and so on.

def retrieve_input():
input = self.myText_Box.get("1.0",'end-1c')

how to get input from a tkinter text widget when its inside a function

You can use Object orient methodology to make access sympler, another option also you can use globals() to make variable global

One way

globals()['user_message_entry'] = tk.Text(message_frame, height=10, width=60)

....

and from another function you can call

body = globals()['user_message_entry'].get("1.0", "end")

Second way

Object oriented programming is good for every type of problem solving, so you can use class as well

import tkinter as tk

class CBased:
def __init__(self, master, *args, **kwargs):
super(CBased, self).__init__(*args, *kwargs)
self.master = master
master.geometry("500x500")

self.open_feedback_button = tk.Button(master, command=self.feedback, height=20, width=20, bg="yellow", text="open feedback window")
self.open_feedback_button.grid(row=1, column=0)

def send_email(self):
subject = ":)"
body = self.user_message_entry.get("1.0", "end")
message = f"subject: {subject}\n\n {body}"
print(message)

def feedback(self):
self.feedback_window = tk.Toplevel()
self.feedback_window.geometry("690x650")

self.message_frame = tk.Frame(self.feedback_window)
self.message_frame.grid(row=0, column=0, columnspan=3)
self.user_message_entry = tk.Text(self.message_frame, height=10, width=60)
self.user_message_entry.grid(row=0, column=0)

self.send_email_button = tk.Button(self.feedback_window, command=send_email,
height=20, width=20, bg="yellow", text="send email")
self.send_email_button.grid(row=1, column=0)

def main():
root = Tk()
myobj = CBased(root)
root.mainloop()

if __name__ == "__main__":main()

In this way you can call every single item by self.xyz

How to get the content of a tkinter Text object

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.

Manipulating user input from tkinter text widget

Thanks Matiiss!! Posting the answer so it shows as resolved. If you would post it so I can accept your answer and upvote it!

result= text_widget.get("1.0", "end-1c").split("\n")
for line in result:
func_1():

Can't get text input from Tkinter Text widget into sqlite3 database

The second argument of cursor.execute(...) is expected to be a tuple or list, so change

cursor.execute("INSERT INTO 'sponsors' (sponsor) VALUES(?)", sponsor.get('1.0', 'end'))

to

cursor.execute("INSERT INTO 'sponsors' (sponsor) VALUES(?)", [sponsor.get('1.0', 'end')])

How to restrict inputting text only for a specified time in Tkinter Text widget

You can bind <key> event to a function, then inside the callback to disable the text box 5 seconds later using .after().

from tkinter import *

my_window = Tk()

type_txt = Text()
type_txt.grid(row=0, column=0)
type_txt.focus()

def disable_textbox():
type_txt.configure(state=DISABLED)
typed_text = type_txt.get("1.0", END)
print(typed_text)

def start_typing(event):
# disable <Key> binding
type_txt.unbind('<Key>')
# disable text box 5 seconds later
type_txt.after(5000, disable_textbox)

type_txt.bind('<Key>', start_typing)
my_window.mainloop()

Print all outputs (in realtime) in text widget in Tkinter

Change your RedirectText write function to update the widget after writing to it.

class RedirectText(object):
def __init__(self, text_widget):
"""Constructor"""
self.output = text_widget

def write(self, string):
"""Add text to the end and scroll to the end"""
self.output.insert('end', string)
self.output.see('end')
self.output.update_idletasks()



Related Topics



Leave a reply



Submit