By Using Procedural Programming Paradigm in Python, I have a written a program which reads the input of a file(say File.txt) line by line and print it. Below is the Example:
Script:
import fileinput
for line in fileinput.input(r'D:\File.txt'):
line = line.replace(" ", "")
line = line[0:-1]
print(line)
Result:
Note: If for example "File.txt" contains 2 lines, First line as 'line1' and Second line as 'line2', then output is:
line1
line2
Same Result I want through Object Oriented programing Paradigm by using "Tkinter Multiline TextBox" instead of a file(here in above example File.txt).
I have the Below Code that creates a MultiLine Tkinter TextBox:
import tkinter as tki # Tkinter -> tkinter in Python3
class App(object):
def __init__(self):
self.root = tki.Tk()
# create a Frame for the Text and Scrollbar
txt_frm = tki.Frame(self.root, width=200, height=200)
txt_frm.pack(fill="both", expand=True)
# ensure a 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 a 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 a Scrollbar and associate it with 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 My Problem is, once I run the above code a "Tkinter Multiline TextBox" is coming and I am entering 4 Lines in the Tkinter TextBox as:
ABC
XYZ
PQR
QAZ
But I am not getting the Exact Idea/Implementation that How I can read those lines form Tkinter TextBox one by one and Use them for further processing in My Program.
Python Version I am using is 3.0.1. Please Help...
From the document, the get method on tkinter.text will just return the string, including the new line \n. You can not treat the tkinter.text as file, but you can use other ways.
Read them all and split them into a list. Loop the list then.
def retrieve_input():
text = self.txt.get('1.0', END).splitlines()
for line in text:
...
Using io.StringIO to emulate a file, but in this case it will not strip the newline.
def retrieve_input():
text = io.StringIO(self.txt.get('1.0', END))
for line in text:
line = line.rstrip()
...
I modifed your code.
retrieve_input on click.self parameter.You can get text using self.txt.get("1.0", tki.END). Use str.splitlines() to get lines as list.
import tkinter as tki
class App(object):
def __init__(self):
self.root = tki.Tk()
# create a Frame for the Text and Scrollbar
txt_frm = tki.Frame(self.root, width=200, height=200)
txt_frm.pack(fill="both", expand=True)
# ensure a 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 a 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 a Scrollbar and associate it with txt
scrollb = tki.Scrollbar(txt_frm, command=self.txt.yview)
scrollb.grid(row=0, column=1, sticky='nsew')
self.txt['yscrollcommand'] = scrollb.set
tki.Button(txt_frm, text='Retrieve input', command=self.retrieve_input).grid(row=1, column=0)
def retrieve_input(self):
lines = self.txt.get("1.0", tki.END).splitlines()
print(lines)
app = App()
app.root.mainloop()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With