Skip to content Skip to sidebar Skip to footer

How To Save Text From A Text-widget Tkinter To A .doc Using Asksaveasfile?

I want to save some stuff I entered in a Text widget to a .txt or .doc format, using an asksaveasfilename dialog box. After this I want it to open in resp. Notepad or MS Word. from

Solution 1:

You can see the problem if you add print name, e.g.

C:/Users/jsharpe/Downloads/testing

note that no extension has been added - I only entered "testing". The filetypes argument is more useful for limiting the user's selection of existing files, it won't add the appropriate extension if the user doesn't supply one.

You can set a defaultextension for the cases where the user doesn't enter an extension, but this won't reflect the type selected in the drop-down (e.g. if you set defaultextension=".txt" it won't be .doc even if the user chooses that option from filetypes).

name = asksaveasfilename(defaultextension=".txt",
                         filetypes=[("Text files",".txt"),
                                    ("Word files",".doc")],
                         initialdir="dir",
                         title="Save as")

(Note that alphabetical argument order makes life easier as you add more and more options)


On a side note, you (still!) don't close the file, which could cause issues - I'd suggest using the with context manager for file handling:

with open(name, "w") as data:
    data.write("text from text widget")

Solution 2:

Just run this code it will work:

from tkinter import *
from tkinter import ttk
name=[]*1000
surname=[]*1000
email=[]*1000
password=[]*1000
cpassword=[]*1000
phonenum=[]*1000
def mainpage3():
    global phonenum
    global cpassword
    global password
    global email
    global surname
    global name
    global page1
    global bpage
    global e1
    global e2
    global e3
    global e4
    global e5
    global e6 
    name.append(e1.get())
    surname.append(e2.get())
    email.append(e3.get())
    password.append(e4.get())
    cpassword.append(e5.get())
    phonenum.append(e6.get())
    bpage.destroy()
    cpage=Tk()
    cpage.title('boors')
    mainframe = ttk.Frame(cpage, padding="3 3 12 12")
    mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
    mainframe.columnconfigure(0, weight=1)
    mainframe.rowconfigure(0, weight=1)
    ttk.Label(cpage,text='نام').grid(column=8,row=1)
    ttk.Label(cpage,text='نام خانوادگي').grid(column=8,row=2)
    ttk.Label(cpage,text='ايميل').grid(column=8,row=3)
    ttk.Label(cpage,text='شماره تلفن').grid(column=8,row=4)
    ttk.Label(cpage,text=name[0]).grid(column=7,row=1)
    ttk.Label(cpage,text=surname[0]).grid(column=7,row=2)
    ttk.Label(cpage,text=email[0]).grid(column=7,row=3)
    ttk.Label(cpage,text=phonenum[0]).grid(column=7,row=4)
    ttk.Label(cpage,text='').grid(column=8,row=5)
    ttk.Button(cpage,text='خريد').grid(column=8,row=6)
    ttk.Button(cpage,text='فروش').grid(column=8,row=7)
    ttk.Entry(cpage).grid(column=7,row=6)
    ttk.Entry(cpage).grid(column=7,row=7)
    ttk.Label(cpage,text='agah').grid(column=1,row=1)
    ttk.Label(cpage,text='agah').grid(column=2,row=1)
    ttk.Label(cpage,text='agah').grid(column=3,row=1)
    ttk.Label(cpage,text='agah').grid(column=4,row=1)
    ttk.Label(cpage,text='code').grid(column=5,row=1)
    ttk.Label(cpage,text='agah').grid(column=1,row=2)
    ttk.Label(cpage,text='agah').grid(column=2,row=2)
    ttk.Label(cpage,text='agah').grid(column=3,row=2)
    ttk.Label(cpage,text='agah').grid(column=4,row=2)
    ttk.Label(cpage,text='1').grid(column=5,row=2)
    ttk.Label(cpage,text='agah').grid(column=1,row=3)
    ttk.Label(cpage,text='agah').grid(column=2,row=3)
    ttk.Label(cpage,text='agah').grid(column=3,row=3)
    ttk.Label(cpage,text='agah').grid(column=4,row=3)
    ttk.Label(cpage,text='2').grid(column=5,row=3)
    ttk.Label(cpage,text='agah').grid(column=1,row=4)
    ttk.Label(cpage,text='agah').grid(column=2,row=4)
    ttk.Label(cpage,text='agah').grid(column=3,row=4)
    ttk.Label(cpage,text='agah').grid(column=4,row=4)
    ttk.Label(cpage,text='3').grid(column=5,row=4)
    ttk.Label(cpage,text='agah').grid(column=1,row=5)
    ttk.Label(cpage,text='agah').grid(column=2,row=5)
    ttk.Label(cpage,text='agah').grid(column=3,row=5)
    ttk.Label(cpage,text='agah').grid(column=4,row=5)
    ttk.Label(cpage,text='4').grid(column=5,row=5)
    ttk.Label(cpage,text='اخبار').grid(column=3,row=6)
    ttk.Label(cpage,text=' بگو آگاه اخبار').grid(column=3,row=7)
    ttk.Label(cpage,text='     ').grid(column=2,row=6)
def mainpage2():
    global e1
    global e2
    global e3
    global e4
    global e5
    global e6
    global bpage
    page1.destroy()
    bpage=Tk()
    bpage.title('boors')
    bpage.geometry('1000x1000')

    mainframe = ttk.Frame(bpage, padding="3 3 12 12")
    mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
    mainframe.columnconfigure(0, weight=1)
    mainframe.rowconfigure(0, weight=1)
    ttk.Label(bpage,text='').grid(column=1,row=1,sticky=E)
    ttk.Label(bpage,text='').grid(column=1,row=3,sticky=E)
    ttk.Label(bpage,text='').grid(column=1,row=4,sticky=E)
    ttk.Label(bpage,text='').grid(column=1,row=6,sticky=E)
    ttk.Label(bpage,text='').grid(column=1,row=7,sticky=E)
    ttk.Label(bpage,text='').grid(column=1,row=9,sticky=E)
    ttk.Label(bpage,text='').grid(column=1,row=10,sticky=E)
    ttk.Label(bpage,text='').grid(column=1,row=12,sticky=E)
    ttk.Label(bpage,text='').grid(column=1,row=13,sticky=E)
    ttk.Label(bpage,text='').grid(column=1,row=15,sticky=E)
    ttk.Label(bpage,text='').grid(column=1,row=16,sticky=E)
    ttk.Label(bpage,text='').grid(column=1,row=18,sticky=E)
    ttk.Label(bpage,text='').grid(column=1,row=19,sticky=E)
    ttk.Label(bpage,text='Name',font='Verdana 25 bold').grid(column=1,row=2,sticky=E)
    e1=ttk.Entry(bpage,width=100)
    e1.grid(column=2,row=2,sticky=E)
    ttk.Label(bpage,text='Surname',font='Verdana 25 bold').grid(column=1,row=5,sticky=E)
    e2=ttk.Entry(bpage,width=100)
    e2.grid(column=2,row=5,sticky=E)
    ttk.Label(bpage,text='Email',font='Verdana 25 bold').grid(column=1,row=8,sticky=E)
    e3=ttk.Entry(bpage,width=100)
    e3.grid(column=2,row=8,sticky=E)
    ttk.Label(bpage,text='Password',font='Verdana 25 bold').grid(column=1,row=11,sticky=E)
    e4=ttk.Entry(bpage,width=100)
    e4.grid(column=2,row=11,sticky=E)
    ttk.Label(bpage,text='confirm password',font='Verdana 25 bold').grid(column=1,row=14,sticky=E)
    e5=ttk.Entry(bpage,width=100)
    e5.grid(column=2,row=14,sticky=E)
    ttk.Label(bpage,text='phone number',font='Verdana 25 bold').grid(column=1,row=17,sticky=E)
    e6=ttk.Entry(bpage,width=100)
    e6.grid(column=2,row=17,sticky=E)    
    ttk.Button(bpage,text='done',width=100,command=mainpage3).grid(column=2,row=20,sticky=E)

def mainpage():
    global page1
    page1=Tk()
    page1.title('bours')
    mainframe = ttk.Frame(page1, padding="3 3 12 12")
    mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
    mainframe.columnconfigure(0, weight=1)
    mainframe.rowconfigure(0, weight=1)
    ttk.Label(page1,text='Register Here',font = "Verdana 80 bold").grid(column=1,row=2)
    ttk.Label(page1,text='',font='Verdana 30 bold').grid(column=1,row=1,sticky=E)
    ttk.Button(page1,text='register',width=170,command=mainpage2).grid(column=1,row=4,sticky=E)
    ttk.Label(page1,text='',font='Verdana 30 bold').grid(column=1,row=3,sticky=E)
mainpage()

Post a Comment for "How To Save Text From A Text-widget Tkinter To A .doc Using Asksaveasfile?"