Search code examples
pythonpython-2.7filetkinterttk

python tkinter interface how to create a new to show txt file


I buid a code which takes python user input and insert it into a text file when pressing apply as shown in the picture bellow

GUI

and the text file will always be updated when the user inserts a new text, how to create an new button next to apply to show up to date text file to the user

enter image description here and want prevent to enter the same text example if the text file has a (go) the program do not enter (go) again this is my code

root = Tk()

ivn = StringVar()
inputVarName = Entry(root, textvariable=str(ivn))
ivn.set(str("text1"))
inputVarName.grid(row=0, column=0)

ivn2 = StringVar()
inputVarName2 = Entry(root, textvariable=str(ivn2))
ivn2.set(str("text2"))
inputVarName2.grid(row=1, column=0)




def writetofile():
    content_list = [ivn.get(), ivn2.get()]

    print("\n".join(content_list))    
    with open("help.txt", "a") as f:
        for item in content_list:
            f.write("%s\n" % item)

applyButton = Button(root, text="Apply", command=writetofile)
applyButton.grid(row=2, column=1)



root.mainloop()

Solution

  • I was trying something same but with one output

    import tkinter.ttk as ttk
    import tkMessageBox
    
    root = Tk()
    root.geometry("500x300")
    root.title("The Gatway company")
    
    ivn = StringVar()
    inputVarName = Entry(root, textvariable=str(ivn))
    ivn.set(str("text1"))
    inputVarName.grid(row=0, column=0,columnspan=2)
    def writetofile():
        content_list = [ivn.get()]
        if any(content_list):
    
    
            with open("help.txt", 'r+') as inFile:
                for item in content_list:
                    if ("%s\n" % item).encode('UTF-8') in inFile:
                        tkMessageBox.showwarning("Warning", "'%s': Already exists!" % item)
                        return
    
            with open("help.txt", "a") as f:
                for item in content_list:
                    f.write( ("%s\n" % item).encode('UTF-8'))
    
        ivn.set('')
    
    def showfile():
        top = Toplevel()
        top.title("help.txt")
        textArea = Text(top)
    
        scrollbar = ttk.Scrollbar(top, command=textArea.yview)
        scrollbar.grid(row=0, column=1, sticky='nsew')
    
        textArea['yscrollcommand'] = scrollbar.set
    
        with open("help.txt", "r") as infile:
            textArea.insert(END, infile.read())
    
        textArea.grid(row=0, column=0)
        textArea.config(state=DISABLED)
    
    applyButton = Button(root, text="Apply", command=writetofile)
    applyButton.grid(row=2, column=0, sticky=W+E)
    applyButton.config( height = 5, width = 10 )
    
    showButton = Button(root, text="Show", command=showfile)
    showButton.grid(row=2, column=1, sticky=W+E)
    showButton.config( height = 5, width = 10 ) 
    
    
    
    root.mainloop()
    
    

    it's same as your code but for one entry, I'm thinking to edit it in a such way that the user chooses the help.txt file like a file requester.