Python Program to Create Save Dialog Window

Python course with 57 real-time projects - Learn Python

Program 1

from tkinter import *
import tkinter.filedialog as fd
import tkinter.messagebox as msgbox
def btnclick(choice):
    t=Text(myroot,width=100,height=100,fg='red')
    t.pack()
    if(choice=='new'):
       print("This is New Option")
    elif(choice=='save'):
        txtdata=str(t.get(1.0,END))
        filename=fd.asksaveasfilename(parent=myroot,defaultextension="*.txt")
        f=open(filename,'w')
        f.write(txtdata)
        f.close()
    elif(choice=='exit'):
         ch=msgbox.askyesno('exit',"Are you sure want to exit")
         if(ch==True):
           myroot.destroy()
myroot=Tk()
myroot.geometry('800x500')
myroot.title("Save Window Demo")
myroot.wm_iconbitmap('2.ico')
menubar=Menu(myroot)
myroot.config(menu=menubar)
filemenu=Menu(myroot,tearoff=0)
filemenu.add_command(label='New',accelerator="CTRL+N",command=lambda :btnclick('new'))
filemenu.bind('<Control-n>',btnclick('new'))
filemenu.add_command(label='Open',command=lambda :btnclick('open'))
filemenu.add_command(label='Save',command=lambda :btnclick('save'))
filemenu.add_separator()
filemenu.add_command(label='Exit',command=lambda :btnclick('exit'),accelerator="CTRL+Q")
menubar.add_cascade(label='File',menu=filemenu)
myroot.mainloop()

 

Your opinion matters
Please write your valuable feedback about DataFlair on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *