Python Forum
[Tkinter] Get the last entry in my text widget
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Get the last entry in my text widget
#1
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.
Reply
#2
Have a look at this link.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
#! /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()
Pedroski55 likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#4
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: could not convert string to float: '' fron Entry Widget russellm44 5 646 Mar-06-2024, 08:42 PM
Last Post: russellm44
  [Tkinter] entry widget DPaul 5 1,518 Jul-28-2023, 02:31 PM
Last Post: deanhystad
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,854 Jun-26-2022, 06:26 PM
Last Post: menator01
  Tkinter Exit Code based on Entry Widget Nu2Python 6 2,987 Oct-21-2021, 03:01 PM
Last Post: Nu2Python
  [Tkinter] Text widget inert mode on and off rfresh737 5 3,860 Apr-19-2021, 02:18 PM
Last Post: joe_momma
  Line numbers in Text widget rfresh737 3 5,404 Apr-15-2021, 12:30 PM
Last Post: rfresh737
  tkinter text widget word wrap position chrisdb 6 7,566 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  method to add entries in multi columns entry frames in self widget sudeshna24 2 2,247 Feb-19-2021, 05:24 PM
Last Post: BashBedlam
  Entry Widget issue PA3040 16 6,836 Jan-20-2021, 02:21 PM
Last Post: pitterbrayn
  [Tkinter] password with Entry widget TAREKYANGUI 9 5,914 Sep-24-2020, 05:27 PM
Last Post: TAREKYANGUI

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020