i am using python(tkinter) to create my text editing software,but my savefile method acts like a saveas method.
everytime i save it does not overwrite but create a new file instead.
python code below:
Edit admin:
Learn to use code tag(BBCode help).
everytime i save it does not overwrite but create a new file instead.
python code below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
from tkinter import * from tkinter.filedialog import askopenfile from tkinter.filedialog import asksaveasfile filename = None def newfile(): global filename filename = ( "Not Set" ) text.delete( 0.0 ,END) print (filename) def openfile(): f = askopenfile(mode = 'r' ) t = f.read() text.delete( 0.0 ,END) text.insert( 0.0 ,t) print (f) def savefile(): global filename t = text.get( 0.0 ,END) f = open (filename, 'w' ) f.write(t) print (f) f.close() def saveas(): global filename f = asksaveasfile(mode = 'w' ,defaultextension = '.txt' ) t = text.get( 0.0 ,END) try : f.write(t.rstrip()) print (f) except : showerror(title = "saveError" ) root = Tk() root.title( "codiditor" ) root.minsize(width = 400 ,height = 400 ) root.maxsize(width = 400 ,height = 400 ) text = Text(root,width = 400 ,height = 400 ) text.pack() menubar = Menu(root) filemenu = Menu(menubar) filemenu.add_command(label = "New texter" ,command = newfile) filemenu.add_command(label = "Open texter" ,command = openfile) filemenu.add_command(label = "Save texter" ,command = savefile) filemenu.add_command(label = "Save as .codime" ,command = saveascodime) filemenu.add_separator() filemenu.add_command(label = "Say bye" ,command = root.quit) menubar.add_cascade(label = "File" ,menu = f... root.config(menu = menubar) root.mainloop() how to make overwrite an existing file (the currently opened file )? |
Learn to use code tag(BBCode help).