
| New : | To create a file with default name of untitle.txt |
| Open : | Display the filedialog by askopefilename function asking user to select file. |
| Save : | Save data entered in Text widget to file, display the Save As file dialog if the file name is untitle.txt. |
| Save As : | File dialog to save the file as different file name. |
| Close: | Save the file ( above Save Operation ) and remove the content from Text widget. |
| Exit : | Close and exit the editor. ( With file save operation ) |
import tkinter as tk
from tkinter import filedialog,END
from tkinter.filedialog import asksaveasfilename
my_w = tk.Tk()
my_w.geometry("409x350")
init_dir='D:\\testing\\file-menu\\' # folder to work
global file_name # global variable to store file name.
def my_fun():
pass
menubar = tk.Menu(my_w)
my_font1=('Times',12,'bold')
menu_f = tk.Menu(menubar,title='my title',tearoff=0) # file
menubar.add_cascade(label="File", menu=menu_f) # Top Line
menu_f.add_command(label="New",command=lambda:new_file())
menu_f.add_command(label="Open..",command=lambda:open_file())
menu_f.add_command(label="Save",command=lambda:save_file())
menu_f.add_command(label="Save As..",command=lambda:save_as_file())
menu_f.add_command(label="Close",command=lambda:close_file())
menu_f.add_command(label="Exit",command=my_w.quit)
my_w.config(menu=menubar) # adding menu to window
t1 = tk.Text(my_w, height=15, width=45) # added one text box
t1.grid(row=1,column=1,padx=30,pady=20)
my_w.mainloop()
def new_file():
global file_name
file_name='untitle.txt' # new file name
my_w.title(file_name) # Title of the window shows file name
def open_file():
file = filedialog.askopenfilename(filetypes=[("txt file",".txt")],
defaultextension=".txt",initialdir=init_dir)
global file_name
file_name=file # set the file name
my_w.title(file_name) # update the GUI title
fob=open(file,'r') # open in read mode
my_str1=fob.read() # read data from file & store in variable
t1.delete('1.0',END) # remove the previous content in text box
t1.insert(tk.END, my_str1) # add new data from file to text box
fob.close()
def save_file():
global file_name # collect the file name
if(file_name=='untitle.txt'): #if default file name is still there
save_as_file() # call the function
else:
fob=open(file_name,'w') # open in write mode
my_str1=t1.get("1.0",END) # collect the data from text widget
fob.write(my_str1) # write to file
def save_as_file():
file = filedialog.asksaveasfilename(
filetypes=[("txt file", ".txt")],
defaultextension=".txt",initialdir=init_dir)
if file: # if user has not cancelled the dialog to save
fob=open(file,'w') # open the file in write mode
my_str1=t1.get("1.0",END) # collect data from text widget
fob.write(my_str1) # write to file
my_w.title(file) # Update the GUI title with file name
fob.close() # Close file pointer
else: # user has cancelled the operation
print("No file chosen")
def close_file():
save_file() # remove this line if not required
t1.delete('1.0',END) # remove the content from text widget
my_w.title('') # remove the title of GUI
OptionMenu Projects in Tkinter
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.