I'm making a simple text editor and whenever I try to save a file it always gives me this error.
I'm saving it as a normal text file and I cant find the reason why this is happening. I've tried saving it to a different location, made some debugging attempts. Nothing. Could someone help me out?
Code:
from tkinter import*
from tkinter import filedialog
from tkinter import font
root = Tk()
root.title("NewNotepad")
root.geometry("1200x660")
def new_file():
my_text.delete("1.0", END)
root.title('New File')
status_bar.config(text="New File")
def open_file():
my_text.delete("1.0", END)
text_file = filedialog.askopenfilename(title="Open File", filetypes=(("Text Files", "*txt"), ("HTML Files", "*html")))
name = text_file
status_bar.config(text=name)
text_file = open(text_file, 'r')
stuff = text_file.read()
my_text.insert(END, stuff)
text_file.close()
def save_as_file():
text_file = filedialog.asksaveasfilename(defaultextension=".*", title="Save File", initialdir="C:/gui/", filetypes=(("Text Files", "*txt"), ("HTML Files", "*html")))
if text_file:
name = text_file
name = name.replace("C:/gui/", "")
root.title(f'{name} - NewNotepad')
text_file = open(text_file, 'w')
text_file.write(my_text.get(1.0, END))
text_file.close()
my_frame = Frame(root)
my_frame.pack(pady=5)
text_scroll = Scrollbar(my_frame)
text_scroll.pack(side=RIGHT, fill=Y)
my_text = Text(my_frame, width=97, height=25, font=("Helvetica", 16), selectbackground="Light Blue", selectforeground="Black", undo=True, yscrollcommand=text_scroll.set)
my_text.pack()
text_scroll.config(command=my_text.yview)
my_menu = Menu(root)
root.config(menu=my_menu)
file_menu = Menu(my_menu, tearoff=False)
my_menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="New", command=new_file)
file_menu.add_command(label="Open", command=open_file)
file_menu.add_command(label="Save")
file_menu.add_command(label="Save As", command=save_as_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=root.quit)
edit_menu = Menu(my_menu, tearoff=False)
my_menu.add_cascade(label="Edit", menu=edit_menu)
edit_menu.add_command(label="Cut")
edit_menu.add_command(label="Copy")
edit_menu.add_separator()
edit_menu.add_command(label="Undo")
edit_menu.add_command(label="Redo")
status_bar = Label(text="Ready", anchor=E)
status_bar.pack(fill=X, side=BOTTOM, ipady=5)
root.mainloop()
It may be because you used defaultextension=".*" in asksaveasfilename(). When you enter a filename without extension, .* will be appended to the filename and so the filename is an invalid filename.
Change defaultextension=".*" to defaultextension=".txt", for example, if you want to save the file with extension .txt if you do not enter file extension in the file dialog.
Related
So I am creating a text editor using TKinter. I have the basics down and now I am trying to create a button where when you press it, you select a file and it opens it. From what I have seen the insert method would be best for Entry widget. But i am using Label widget and it does not work for some reason. Here is my code so far:
import sys
import tkinter as tk
frame = tk.Tk()
frame.title("Fake Google Docs")
frame.geometry('800x600')
inputtxt = tk.Text(frame,
height = 25,
width = 90)
inputtxt.pack()
global file
#this is function to be executed when u press first button
def printInput():
filename = input("Name of the file make sure you end with .txt")
file = open(filename, 'w')
result=inputtxt.get("1.0","end")
file.write(result)
file.close()
#this is function to be executed for open new file thing
def printInputnew():
filenamemm = input("Name of the file make sure you end with .txt")
file = open(filenamemm, 'w')
result=inputtxt.get("1.0","end")
file.write("")
file.close()
def openfile():
filenamem = input("Name of the file you want to open")
tru=open(filenamem)
if tru== False:
sys.exit()
else:
f = open(filenamem, "r")
for x in f:
print(x)
name=str(x)
text = tk.StringVar()
text.set(name)
#button 1
printButton = tk.Button(frame,
text = "Save to text file",
command = printInput)
printButton.pack()
printButtonl = tk.Button(frame,
text = "open a file",
command = openfile)
printButtonl.pack()
#button 2
printButton = tk.Button(frame,
text = "Open new file",
command = printInputnew)
printButton.pack()
#open the text box
lbl = tk.Label(frame, textvariable = "")
lbl.pack()
#finish code
frame.mainloop()
Could someone else suggest a method for inserting text into this widget?
Here it is:
import sys
import tkinter as tk
from tkinter import filedialog
frame = tk.Tk()
frame.title("Fake Google Docs")
frame.geometry('800x600')
inputtxt = tk.Text(frame,
height=25,
width=90)
inputtxt.pack()
global file
mask = [("Text and Python files", "*.txt *.py *.pyw *.rcad"),
("HTML files", "*.htm"),
("All files", "*.*")]
# this is function to be executed when u press first button
def printInput():
filename = input("Name of the file make sure you end with .txt")
file = open(filename, 'w')
result = inputtxt.get("1.0", "end")
file.write(result)
file.close()
# this is function to be executed for open new file thing
def printInputnew():
filenamemm = input("Name of the file make sure you end with .txt")
file = open(filenamemm, 'w')
result = inputtxt.get("1.0", "end")
file.write("")
file.close()
def openfile():
filenamem = filedialog.askopenfilename(filetypes=mask)
tru = open(filenamem)
if tru == False:
sys.exit()
else:
f = open(filenamem, "r")
inputtxt.delete(0.0, 'end')
inputtxt.insert('end', f.read())
# button 1
printButton = tk.Button(frame,
text="Save to text file",
command=printInput)
printButton.pack()
printButtonl = tk.Button(frame,
text="open a file",
command=openfile)
printButtonl.pack()
# button 2
printButton = tk.Button(frame,
text="Open new file",
command=printInputnew)
printButton.pack()
# open the text box
lbl = tk.Label(frame, textvariable="")
lbl.pack()
# finish code
frame.mainloop()
I changed this block of code:
def openfile():
filenamem = filedialog.askopenfilename(filetypes=mask)
tru = open(filenamem)
if tru == False:
sys.exit()
else:
f = open(filenamem, "r")
inputtxt.delete(0.0, 'end')
inputtxt.insert('end', f.read())
It opens a window that ask you which file you want to open. askopenfilename method returns a string path to selected file. Then you just read whole file, delete whole text that you had in your tk.Text object and paste there text from opened file. That is all :)
And please, next time watch some guides about tkinter before asking and read about PEP8.
I am new to python and Tkinter so I might be making a simple mistake here, but it's for a school project, therefore, I do not have much time to learn everything. Anyways I am making an app with Tkinter and I wanted to split up the code into different python files, functions in one file and accessing them from the main. I have been trying to figure out what's wrong for 3days now and it looks like a very simple mistake but I don't know what it is.
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\mcdd1\AppData\Local\Programs\Python\Python39\lib\tkinter_init_.py", line 1884, in call
return self.func(*args)
File "d:\mcdd1\Desktop\UNI\Switch-Frames-Template-master\textfunc.py", line 22, in open_file
my_text.delete("1.0", END)
NameError: name 'my_text' is not defined
these are the codes that I've used:
the main file is mainnav.py,
from textfunc import open_file
toolbar_frame = Frame(frame2, bg="blue")
toolbar_frame.pack(fill=X)
my_frame = Frame(frame2)
my_frame.pack(pady=5)
my_text = Text(my_frame, width=97, height=25, font=("Helvetica", 16), selectbackground="yellow", selectforeground="black", undo=True, wrap="none")
my_text.pack()
open_button = Button(toolbar_frame, text="Open", command=open_file)
open_button.grid(row=0, column=0, sticky=W, padx=5, pady=5)
the file with the functions textfunc.py (in the same directory)
global open_status_name
open_status_name = False
global selected
selected = False
def open_file():
my_text.delete("1.0", END)
text_file = filedialog.askopenfilename(initialdir="", title="Open File", filetypes=(("Text Files", "*.txt"), ("All Files", "*.*")))
if text_file:
global open_status_name
open_status_name = text_file
text_file = open(text_file, 'r')
stuff = text_file.read()
my_text.insert(END, stuff)
text_file.close()
these are just the snippets of code I think would be needed to troubleshoot the problem. any help would be greatly appreciated!
The my_text variable is not defined in your mainnav.py file. You can pass it as an argument to the open_file() function if you want to access it:
mainnav.py:
from textfunc import open_file
toolbar_frame = Frame(frame2, bg="blue")
toolbar_frame.pack(fill=X)
my_frame = Frame(frame2)
my_frame.pack(pady=5)
my_text = Text(my_frame, width=97, height=25, font=("Helvetica", 16), selectbackground="yellow", selectforeground="black", undo=True, wrap="none")
my_text.pack()
open_button = Button(toolbar_frame, text="Open", command=lambda: open_file(my_text))
open_button.grid(row=0, column=0, sticky=W, padx=5, pady=5)
textfunc.py:
def open_file(my_text):
my_text.delete("1.0", END)
text_file = filedialog.askopenfilename(initialdir="", title="Open File", filetypes=(("Text Files", "*.txt"), ("All Files", "*.*")))
if text_file:
global open_status_name
open_status_name = text_file
text_file = open(text_file, 'r')
stuff = text_file.read()
my_text.insert(END, stuff)
text_file.close()
Note: you need to use lambda for passing argument to the Button's command.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
I will get straight into it.
I want to code a Code Editor for HTML, CSS, and JS with Python Tkinter, but I don't know how to make an automated "closing tag system".
and I also want to know how to make each tag another color, automatically.
I'm really struggling with this because I am also new at python
Here some code:
PYTHON:
from tkinter import *
from tkinter import filedialog
from tkinter import font
root = Tk()
root.title('text-editor - reddrosee')
root.geometry('1200x660')
root.configure(background='white')
#SAVEFILE
def new_file():
my_text.delete("1.0", END)
root.title('New File - reddrosee')
#OPEN FILE
def open_file():
my_text.delete("1.0", END)
text_file = filedialog.askopenfilename(initialdir="C:/reddrosee/", title="Open File", filetypes=(("HTML Files", "*.html"), ("Text Files", "*.txt"), ("Python Files", "*.py"), ("All Files", "*.*")))
name = text_file
root.title(name + ' - reddrosee')
text_file = open(text_file, 'r')
read_text = text_file.read()
my_text.insert(END, read_text)
text_file.close()
def save_as_file():
text_file = filedialog.asksaveasfilename(defaultextension=".*", initialdir="C:/reddrosee/", title="Save File", filetypes=(("HTML Files", "*.html"), ("Text Files", "*.txt"), ("Python Files", "*.py"), ("All Files", "*.*")))
if text_file:
name = text_file
name = name.replace("C:/reddrosee/", "")
root.title(name + ' - reddrosee')
text_file = open(text_file, 'w')
text_file.write(my_text.get(1.0, END))
root.title('SAVED' + ' - reddrosee')
text_file.close()
#CREATE MAIN FRAME
my_frame = Frame(root)
my_frame.pack(pady=5)
#SCROLLBAR(TEXTBOX)
text_scroll = Scrollbar(my_frame)
text_scroll.pack(side=RIGHT, fill=Y)
#CREATE TEXTBOX
myFont = font.Font(family='monospace')
my_text = Text(my_frame, width=1920, height=1080, font=(myFont, 16), bg="white",selectbackground="silver", fg="black" ,selectforeground="white", undo=True, yscrollcommand=text_scroll.set)
my_text.pack()
#CONFIG SCROLLBAR
text_scroll.config(command=my_text.yview)
#MENU
my_menu = Menu(root)
root.config(menu=my_menu)
#FILEMENU
file_menu = Menu(my_menu, tearoff=False)
my_menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="New", command=new_file)
file_menu.add_command(label="Open", command=open_file)
file_menu.add_command(label="Save")
file_menu.add_command(label="Save As", command=save_as_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command= root.quit)
#ADD EDIT MENU
edit_menu = Menu(my_menu, tearoff=False)
my_menu.add_cascade(label="Edit", menu=edit_menu)
edit_menu.add_command(label="Cut")
edit_menu.add_command(label="Copy")
edit_menu.add_command(label="Undo")
edit_menu.add_command(label="Redo")
root.mainloop()
-REDDROSEEEE
Here is one way of doing it. Create your tags in the form of a dictionary and check the last inserted character in the text widget. If the last character matches the key in the dictionary then insert the value at the end. Also bind the text widget with the function:
tags = {'{':'}', '[':']', '(': ')'}# add your auto complete tags here
def autoComplete(*event):
pos = my_text.index(INSERT)
text.set(my_text.get('1.0', END))
inputValue = text.get()[-2:].strip()
backspace = str(event[0])
if 'BackSpace' not in backspace.split()[3]:
if inputValue in list(tags.keys()):
my_text.insert(END, tags[inputValue])
The complete code:
from tkinter import *
from tkinter import filedialog
from tkinter import font
tags = {'{':'}', '[':']', '(': ')'}# add your auto complete tags here
def autoComplete(*event):
pos = my_text.index(INSERT)
text.set(my_text.get('1.0', END))
inputValue = text.get()[-2:].strip()
backspace = str(event[0])
if 'BackSpace' not in backspace.split()[3]:
if inputValue in list(tags.keys()):
my_text.insert(END, tags[inputValue])
root = Tk()
root.title('text-editor - reddrosee')
root.geometry('1200x660')
root.configure(background='white')
#SAVEFILE
def new_file():
my_text.delete("1.0", END)
root.title('New File - reddrosee')
#OPEN FILE
def open_file():
my_text.delete("1.0", END)
text_file = filedialog.askopenfilename(initialdir="C:/reddrosee/", title="Open File", filetypes=(("HTML Files", "*.html"), ("Text Files", "*.txt"), ("Python Files", "*.py"), ("All Files", "*.*")))
name = text_file
root.title(name + ' - reddrosee')
text_file = open(text_file, 'r')
read_text = text_file.read()
my_text.insert(END, read_text)
text_file.close()
def save_as_file():
text_file = filedialog.asksaveasfilename(defaultextension=".*", initialdir="C:/reddrosee/", title="Save File", filetypes=(("HTML Files", "*.html"), ("Text Files", "*.txt"), ("Python Files", "*.py"), ("All Files", "*.*")))
if text_file:
name = text_file
name = name.replace("C:/reddrosee/", "")
root.title(name + ' - reddrosee')
text_file = open(text_file, 'w')
text_file.write(my_text.get(1.0, END))
root.title('SAVED' + ' - reddrosee')
text_file.close()
#CREATE MAIN FRAME
my_frame = Frame(root)
my_frame.pack(pady=5)
#SCROLLBAR(TEXTBOX)
text_scroll = Scrollbar(my_frame)
text_scroll.pack(side=RIGHT, fill=Y)
text = StringVar()
#text.trace('w', autoComplete)
#CREATE TEXTBOX
myFont = font.Font(family='monospace')
my_text = Text(my_frame, width=1920, height=1080, font=(myFont, 16), bg="white",
selectbackground="silver", fg="black" ,selectforeground="white", undo=True,
yscrollcommand=text_scroll.set)
my_text.pack()
my_text.bind('<KeyRelease>', autoComplete)
#CONFIG SCROLLBAR
text_scroll.config(command=my_text.yview)
#MENU
my_menu = Menu(root)
root.config(menu=my_menu)
#FILEMENU
file_menu = Menu(my_menu, tearoff=False)
my_menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="New", command=new_file)
file_menu.add_command(label="Open", command=open_file)
file_menu.add_command(label="Save")
file_menu.add_command(label="Save As", command=save_as_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command= root.quit)
#ADD EDIT MENU
edit_menu = Menu(my_menu, tearoff=False)
my_menu.add_cascade(label="Edit", menu=edit_menu)
edit_menu.add_command(label="Cut")
edit_menu.add_command(label="Copy")
edit_menu.add_command(label="Undo")
edit_menu.add_command(label="Redo")
root.mainloop()
EDIT: you may also use event.keycode instead of slicing and using strip in the autocomplete function. Refer to my second answer.
Ok, I am adding another answer my previous answer works only for the tags at the end.
Use this, this will auto-complete the tags anywhere in the program.
Also, this is better than the previous one.
from tkinter import *
from tkinter import filedialog
from tkinter import font
tags = {'{':'}', '[':']', '(':')'}# add your auto complete tags here
reject_keys = [8, 16, 17, 18, 27, 37, 38, 39, 40]
def autoComplete(event):
pos = f'{float(my_text.index(INSERT))-0.1:1.1f}'
inputValue = my_text.get(pos).strip()
backspace = event.keycode
if backspace not in reject_keys:
if inputValue in list(tags.keys()):
insert_pos = my_text.index(INSERT)
my_text.insert(insert_pos, tags[inputValue])
root = Tk()
root.title('text-editor - reddrosee')
root.geometry('1200x660')
root.configure(background='white')
#SAVEFILE
def new_file():
my_text.delete("1.0", END)
root.title('New File - reddrosee')
#OPEN FILE
def open_file():
my_text.delete("1.0", END)
text_file = filedialog.askopenfilename(initialdir="C:/reddrosee/", title="Open File", filetypes=(("HTML Files", "*.html"), ("Text Files", "*.txt"), ("Python Files", "*.py"), ("All Files", "*.*")))
name = text_file
root.title(name + ' - reddrosee')
text_file = open(text_file, 'r')
read_text = text_file.read()
my_text.insert(END, read_text)
text_file.close()
def save_as_file():
text_file = filedialog.asksaveasfilename(defaultextension=".*", initialdir="C:/reddrosee/", title="Save File", filetypes=(("HTML Files", "*.html"), ("Text Files", "*.txt"), ("Python Files", "*.py"), ("All Files", "*.*")))
if text_file:
name = text_file
name = name.replace("C:/reddrosee/", "")
root.title(name + ' - reddrosee')
text_file = open(text_file, 'w')
text_file.write(my_text.get(1.0, END))
root.title('SAVED' + ' - reddrosee')
text_file.close()
#CREATE MAIN FRAME
my_frame = Frame(root)
my_frame.pack(pady=5)
#SCROLLBAR(TEXTBOX)
text_scroll = Scrollbar(my_frame)
text_scroll.pack(side=RIGHT, fill=Y)
text = StringVar()
#text.trace('w', autoComplete)
#CREATE TEXTBOX
myFont = font.Font(family='monospace')
my_text = Text(my_frame, width=1920, height=1080, font=(myFont, 16), bg="white",
selectbackground="silver", fg="black" ,selectforeground="white", undo=True,
yscrollcommand=text_scroll.set)
my_text.pack()
my_text.bind('<KeyRelease>', autoComplete)
#CONFIG SCROLLBAR
text_scroll.config(command=my_text.yview)
#MENU
my_menu = Menu(root)
root.config(menu=my_menu)
#FILEMENU
file_menu = Menu(my_menu, tearoff=False)
my_menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="New", command=new_file)
file_menu.add_command(label="Open", command=open_file)
file_menu.add_command(label="Save")
file_menu.add_command(label="Save As", command=save_as_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command= root.quit)
#ADD EDIT MENU
edit_menu = Menu(my_menu, tearoff=False)
my_menu.add_cascade(label="Edit", menu=edit_menu)
edit_menu.add_command(label="Cut")
edit_menu.add_command(label="Copy")
edit_menu.add_command(label="Undo")
edit_menu.add_command(label="Redo")
root.mainloop()
I want to be able to control the default restored size of my windows when click the restore button of the window itself. Right now whenever I click the restore button it stays with the maximized size which is the screen resolution but it just moves to the right a little bit. I want in a way that I can set up a default size before dragging it smaller or bigger depending on the desired size. I attached my code for reference
from tkinter import *
from tkinter.filedialog import *
from pathlib import Path
from win32api import GetSystemMetrics
filename = None
file_path = None
files = [
('All Files', '*.*'),
('Python Files', '*.py'),
('Text Document', '*.txt')
]
def newFile():
global filename
filename = "untitled"
text.delete(0.0, END)
def saveFile():
global filename
t = text.get(0.0, END)
try:
f = open(file_path, mode='w')
f.write(t)
f.close()
except:
saveAs()
def saveAs():
f = asksaveasfile(mode='w', initialdir="C:\\Users\\charl/Documents", filetypes=files, defaultextension=".txt")
t = text.get(0.0, END)
f.write(t)
# except:
# f.showerror(title="Oops!", message="Unable to save file...")
def openFile():
global filename, file_path
f = askopenfile(mode='r')
filename = Path(f.name).stem
file_path = f.name
t = f.read()
text.delete(0.0, END)
text.insert(0.0, t)
f.close()
root = Tk()
root.title("My Python Text Editor")
root.state('zoomed')
root.minsize(width=400, height=400)
root.maxsize(width=GetSystemMetrics(0), height=GetSystemMetrics(1))
text = Text(root, width=400, height=400)
text.pack(fill="both", expand=True)
menubar = Menu(root)
file = Menu(root)
filemenu = Menu(menubar, tearoff=False)
filemenu.add_command(label="New", command=newFile)
filemenu.add_command(label="Open", command=openFile)
filemenu.add_command(label="Save", command=saveFile)
filemenu.add_command(label="Save As...", command=saveAs)
filemenu.add_separator()
filemenu.add_command(label="Quit", command=root.quit())
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)
root.mainloop()
It is because the text box is too large. Try removing the width and height options from text = Text(...) like below:
text = Text(root)
Working with Python and Tkinter, I have been trying to find out the way to show the file_path beside the Browse Button but unable to do so.
Here is my code:
import os
from tkFileDialog import askopenfilename
from Tkinter import *
content = ''
file_path = ''
#~~~~ FUNCTIONS~~~~
def open_file():
global content
global file_path
filename = askopenfilename()
infile = open(filename, 'r')
content = infile.read()
file_path = os.path.dirname(filename)
return content
def process_file(content):
print content
#~~~~~~~~~~~~~~~~~~~
#~~~~~~ GUI ~~~~~~~~
root = Tk()
root.title('Urdu Mehfil Ginti Converter')
root.geometry("598x120+250+100")
mf = Frame(root)
mf.pack()
f1 = Frame(mf, width=600, height=250)
f1.pack(fill=X)
f2 = Frame(mf, width=600, height=250)
f2.pack()
file_path = StringVar
Label(f1,text="Select Your File (Only txt files)").grid(row=0, column=0, sticky='e')
Entry(f1, width=50, textvariable=file_path).grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)
Button(f1, text="Browse", command=open_file).grid(row=0, column=27, sticky='ew', padx=8, pady=4)
Button(f2, text="Process Now", width=32, command=lambda: process_file(content)).grid(sticky='ew', padx=10, pady=10)
root.mainloop()
#~~~~~~~~~~~~~~~~~~~
Kindly guide me as how I can show the file path along with the "Browse Button" button after the user selects the file as shown in this image.
Thanks in advance!
First, change this line:
Entry(f1, width=50, textvariable=file_path).grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)
to this:
entry = Entry(f1, width=50, textvariable=file_path)
entry.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)
Then, in the open_file() function, add these two lines, just before the return:
entry.delete(0, END)
entry.insert(0, file_path)
Explanation:
First, we give the entry a name, so that it can be modified.
Then, in the open_file() function we clear it and add the text for the file-path.
Here is a diff that fixes instead the file_path, i.e. the StringVar() usage:
--- old.py 2016-08-10 18:22:16.203016340 +0200
+++ new.py 2016-08-10 18:24:59.115328029 +0200
## -4,7 +4,6 ##
content = ''
-file_path = ''
#~~~~ FUNCTIONS~~~~
## -16,7 +15,7 ##
filename = askopenfilename()
infile = open(filename, 'r')
content = infile.read()
- file_path = os.path.dirname(filename)
+ file_path.set(os.path.dirname(filename))
return content
def process_file(content):
## -40,7 +39,7 ##
f2 = Frame(mf, width=600, height=250)
f2.pack()
-file_path = StringVar
+file_path = StringVar(root)
Label(f1,text="Select Your File (Only txt files)").grid(row=0, column=0, sticky='e')