Python Forum
[Tkinter] Get the last entry in my text widget - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Get the last entry in my text widget (/thread-28293.html)



Get the last entry in my text widget - Pedroski55 - Jul-13-2020

I want to get the last entry in my text widget.

This writes an entry to the text widget (thanks to Menator01):

def printit(text):
        #text_area.delete(0.0, tk.END)
        text_area.insert(tk.END, text.get() + '\n')
I can't see how to assign this directly to a variable. But I can use:

def retrieve_input():
        #input = self.myText_Box.get("1.0",'end-1c')
        answer = text_area.get(X)
to get all the text.

What value must X have to get the last entry, the last line, in the text widget? I need to do this a few times.


RE: Get the last entry in my text widget - menator01 - Jul-13-2020

Have a look at this link.


RE: Get the last entry in my text widget - menator01 - Jul-13-2020

#! /usr/bin/env python3

# Do the imports
import tkinter as tk
from functools import partial

root = tk.Tk()
root['padx'] = 20
root['pady'] = 10

def printit(text, event):
    text_area['state'] = 'normal'
    text_area.insert(tk.END, text.get()+ '\n')
    text_area['state'] = 'disable'
    entry.delete(0, tk.END)

def get_last_line():
    pos = text_area.index('end-1c linestart')
    pos = float(pos)-1
    line = text_area.get(pos, tk.END)
    label['text'] = f'Last line is: {line.strip()}'


frame = tk.Frame(root)
frame.grid(column=0, row=0, sticky='nw', padx=10)

frame2 = tk.Frame(root)
frame2.grid(column=1, row=0, sticky='nw', padx=10)

entry = tk.Entry(frame, width=40)
entry.focus()
entry.grid(column=0, row=0, sticky='n')


text_area = tk.Text(frame2, width=40, height=10)
text_area.grid(column=0, row=0, rowspan=2)

label = tk.Label(frame)
label['text'] = 'Last line is:'
label.grid(column=0, row=2, sticky='w', pady=50)

btn = tk.Button(root, text='Submit', command=partial(printit, entry, event=None))
btn.grid(column=0, row=3, sticky='w')
root.bind('<Return>', partial(printit, entry))

btn = tk.Button(root, text='Get Last Line', command=partial(get_last_line))
btn.grid(column=1, row=3)

root.mainloop()



RE: Get the last entry in my text widget - Pedroski55 - Jul-13-2020

Thank you very much! I strained my brain making this work! Getting the penultimate line was tricky.

Obviously, your code is much better than mine! I don't know about float.

Just out of interest, this is what I did (the extra output is so I can see what is happening, I'll block that later):

def retrieve_input():        
        pos = text_area.index("end-1c")
        lineNum = pos.split('.')        
        line = int(lineNum[0]) - 1
        #echoMessage(str(line))
        pos2 = str(line) + '.0'
        answer = text_area.get(pos2, 'end-1c')        
        echoMessage('answer is ' + answer)
        echoMessage('pos is ' + pos)
        echoMessage('pos2 is ' + pos2)
        echoMessage('line number echoed was ' + str(line))
        return pos2;
Still using the window you made, I added a button 'Submit answer'

My little 'makehtmltable.py' asks me if I want to continue. It needs a 'y' to carry on. Now I can give it a y. This is the output in the text widget

Quote:Enter y to continue, enter nothing to stop.
y
answer is y

Thanks again for your help!