Black Snow
Black Snow

Reputation: 247

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()

Upvotes: 1

Views: 1156

Answers (2)

Kenly
Kenly

Reputation: 26698

To prevent a user from inserting the same text just empty the two entries, and you can check if entries are not empty before saving to a file.

You can use a top-level window to show the file content.

Check the following example:

from tkinter import Tk, Toplevel, Button, Entry, StringVar, Text, DISABLED, END, W, E 
import tkinter.ttk as ttk
import tkMessageBox

root = Tk()


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

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


def writetofile():
    content_list = [ivn.get(), ivn2.get()]
    if any(content_list):
        print("\n".join(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('')
    ivn2.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)

showButton = Button(root, text="Show", command=showfile)
showButton.grid(row=2, column=1, sticky=W+E)



root.mainloop()

Edited to answer @IbrahimOzaeri question in comments.
You can use tkFileDialog.askopenfilename to ask user to select a file:

from Tkinter import Tk
import Tkinter, Tkconstants, tkFileDialog

root = Tk()
root.filename = tkFileDialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("text files","*.txt"),("all files","*.*")))
print (root.filename)

Upvotes: 2

programming freak
programming freak

Reputation: 899

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.

Upvotes: 1

Related Questions