How to read the input(line by line) from a multiline Tkinter Textbox in Python? -


by using procedural programming paradigm in python, have written program reads input of file(say file.txt) line line , print it. below example:

script:

import fileinput line in fileinput.input(r'd:\file.txt'):     line = line.replace(" ", "")     line = line[0:-1]     print(line) 

result:

note: if example "file.txt" contains 2 lines, first line 'line1' , second line 'line2', output is:

line1 line2 

same result want through object oriented programing paradigm using "tkinter multiline textbox" instead of file(here in above example file.txt).

i have below code creates multiline tkinter textbox:

import tkinter tki # tkinter -> tkinter in python3 class app(object):      def __init__(self):         self.root = tki.tk()      # create frame text , scrollbar         txt_frm = tki.frame(self.root, width=200, height=200)         txt_frm.pack(fill="both", expand=true)          # ensure consistent gui size         txt_frm.grid_propagate(false)          # implement stretchability         txt_frm.grid_rowconfigure(0, weight=1)         txt_frm.grid_columnconfigure(0, weight=1)      # create text widget         self.txt = tki.text(txt_frm, borderwidth=3, relief="sunken")         self.txt.config(font=("consolas", 12), undo=true, wrap='word')         self.txt.grid(row=0, column=0, sticky="nsew", padx=2, pady=2)      # create scrollbar , associate txt         scrollb = tki.scrollbar(txt_frm, command=self.txt.yview)         scrollb.grid(row=0, column=1, sticky='nsew')         self.txt['yscrollcommand'] = scrollb.set      def retrieve_input():         input = self.txt.get("0.0",end)         print(input) app = app() app.root.mainloop() app.retrieve_input() 

now problem is, once run above code "tkinter multiline textbox" coming , entering 4 lines in tkinter textbox as:

abc xyz pqr qaz 

but not getting exact idea/implementation how can read lines form tkinter textbox 1 one , use them further processing in program.

python version using 3.0.1. please help...

from document, get method on tkinter.text return string, including new line \n. can not treat tkinter.text file, can use other ways.

  1. read them , split them list. loop list then.

    def retrieve_input():     text = self.txt.get('1.0', end).splitlines()     line in text:         ... 
  2. using io.stringio emulate file, in case not strip newline.

    def retrieve_input():     text = io.stringio(self.txt.get('1.0', end))     line in text:         line = line.rstrip()         ... 

Comments