python tkinter interface how to create a new to show txt file
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 ttkimport tkMessageBoxroot = 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 Tkimport Tkinter, Tkconstants, tkFileDialogroot = Tk()root.filename = tkFileDialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("text files","*.txt"),("all files","*.*")))print (root.filename)
I was trying something same but with one output
import tkinter.ttk as ttkimport tkMessageBoxroot = 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.