Read Contents of txt file and display in Tkinter GUI Python

16,811

Solution 1

Like this? The changes are just in main function so u need to change only that 1.

def main():
    root = Tk()
    c = Canvas(root,width=600)
    c.pack(side = 'left',expand=1,fill=BOTH)

    c2 = Canvas(c,width=600)
    c2.pack(side = 'left',expand=1,fill=BOTH)
    c3 = Canvas(c,width=600)
    c3.pack(side = 'left',expand=1,fill=BOTH)

    w1 = Label(c2, text="Controller value")
    w1.pack()
    e = Entry(c2)
    e.pack()
    w2 = Label(c2, text="Velocity")
    w2.pack()
    e1 = Entry(c2)
    e1.pack()
    w3 = Label(c2, text="Desired Heading")
    w3.pack()
    e2 = Entry(c2)
    e2.pack()
    toolbar = Frame(c2)
    b = Button(toolbar, text="save", width=9, command=lambda:save(e,e1,e2))
    b.pack(side='left', padx=2, pady=2)
    toolbar.pack(side=TOP, fill=X)

    lt = Label(c3,text='Controller')
    lt.pack(side='top',expand=1,fill='x')
    l = Label(c3,text='',bg='red')
    l.pack(side='top',expand=1,fill='x')
    lt2 = Label(c3,text='Velocity')
    lt2.pack(side='top',expand=1,fill='x')
    l2 = Label(c3,text='',bg='yellow')
    l2.pack(side='top',expand=1,fill='x')
    lt3 = Label(c3,text='Desired Heading')
    lt3.pack(side='top',expand=1,fill='x')
    l3 = Label(c3,text='',bg='blue')
    l3.pack(side='top',expand=1,fill='x')

    b2 = Button(c3,text='load',command=lambda:loadme(l,l2,l3))
    b2.pack(fill='x', padx=2, pady=2)
    root.mainloop()

Solution 2

I don't know if you want something like this, but try:

from Tkinter import *


def save(e,e1,e2):
    open("text.txt","w").close()
    text = e.get() + "\t" + e1.get() + "\t" +  e2.get() + "\t"
    with open("text.txt", "a") as f:
        f.write(text)

def loadme(l,l2,l3):
    f = open('text.txt','r')
    line = f.readline()
    la1,la2,la3 = line.split()
    l.config(text=la1)
    l2.config(text=la2)
    l3.config(text=la3)
    f.close()

def main():
    root = Tk()
    c = Canvas(root,width=600)
    c.pack(side = 'left',expand=1,fill=BOTH)

    c2 = Canvas(c,width=600)
    c2.pack(side = 'left',expand=1,fill=BOTH)
    c3 = Canvas(c,width=600)
    c3.pack(side = 'left',expand=1,fill=BOTH)

    w1 = Label(c2, text="Controller value")
    w1.pack()
    e = Entry(c2)
    e.pack()
    w2 = Label(c2, text="Velocity")
    w2.pack()
    e1 = Entry(c2)
    e1.pack()
    w3 = Label(c2, text="Desired Heading")
    w3.pack()
    e2 = Entry(c2)
    e2.pack()
    toolbar = Frame(c2)
    b = Button(toolbar, text="save", width=9, command=lambda:save(e,e1,e2))
    b.pack(side=LEFT, padx=2, pady=2)
    toolbar.pack(side=TOP, fill=X)

    l = Label(c3,text='',bg='red')
    l.pack(side='left',expand=1,fill='x')
    l2 = Label(c3,text='',bg='yellow')
    l2.pack(side='left',expand=1,fill='x')
    l3 = Label(c3,text='',bg='blue')
    l3.pack(side='left',expand=1,fill='x')

    b2 = Button(c3,text='load',command=lambda:loadme(l,l2,l3))
    b2.pack(fill='x')
    root.mainloop()

if __name__ == '__main__':
    main()
Share:
16,811
Deepak
Author by

Deepak

Updated on June 04, 2022

Comments

  • Deepak
    Deepak almost 2 years

    Hey I have successfully created a tkinter GUI in python which saves the entered values in a text file. Here is the code:

    from Tkinter import *
    root = Tk()
    def save():
        open("text.txt","w").close()
        text = e.get() + "\n" + e1.get() + "\n" +  e2.get() + "\n"
        with open("text.txt", "a") as f:
            f.write(text)
    w1 = Label(root, text="Controller value")
    w1.pack()
    e = Entry(root)
    e.pack()
    w2 = Label(root, text="Velocity")
    w2.pack()
    e1 = Entry(root)
    e1.pack()
    w3 = Label(root, text="Desired Heading")
    w3.pack()
    e2 = Entry(root)
    e2.pack()
    toolbar = Frame(root)
    b = Button(toolbar, text="save", width=9, command=save)
    b.pack(side=LEFT, padx=2, pady=2)
    toolbar.pack(side=TOP, fill=X)
    mainloop()
    

    Now what I want to do is create 3 new textboxes in the GUI which will display the contents of the file. For example my text.txt file has the contents:

    3
    
    2
    
    4
    

    Now I want each of these 3 values to be displayed in 3 textboxes in the GUI. Basically I want the first textbox in the GUI to display 3, second textbox 2 and third textbox 4. Help me out please.