Part of code is adding extra new line when saving to text file. - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: Part of code is adding extra new line when saving to text file. (/thread-20654.html) |
Part of code is adding extra new line when saving to text file. - lovepeace - Aug-23-2019 new to python 3 so bare with me lol I can't for the life of me figure out whats going wrong. the problem is when i use "set goal" it adds a blank line in the save file, so line 1 is a number line 2 is blank and 3 has a number. nether "add miles" or "clear miles" has this problem and they all use the same "save" function. other than that, everything else is working right. P.S. I know my code is sloppy and there's a million better ways to do it. but I'm new :) from tkinter import * import tkinter as tk load=open('save.txt') current = load.readlines()[0] load.seek(0, 0) goal = load.readlines()[1] left = int(goal) - int(current) load.close() rem = int(goal)-int(current) def clear(): global current current = 0 cur.config(text= "Current Miles: " + str(current)) left.config(text="Miles Remaining " + str(goal)) savefile() return def savefile(): with open("save.txt", 'w') as sav: sav.write (str(current)) sav.write("\n") sav.write (str(goal)) sav.close() def addmiles(): global current global rem temp = int(current) add =int(adde.get()) current = (add+temp) rem = int(goal) - int(current) left.config(text= "Miles Remaining: "+ str(rem)) cur.config(text= "Current Miles: " + str(current)) adde.delete(first=0,last=END) savefile() return def setgoal(): global goal goal = int(adde.get()) rem = (goal) - int(current) goallab.config(text= "Your goal is: " + str(goal)+ " Miles.") left.config(text= "Miles Remaining: "+ str(rem)) adde.delete(first=0,last=END) savefile() return master = tk.Tk() master.title("Goal Master v1.0") master.geometry('350x250') add = Button(master, text = "Add", command = addmiles) add.place(x = 250,y = 10) set= Button(master,text = "Set Goal",command = setgoal) set.place(x=250,y=40) clear= Button(master,text="Clear Miles",command =clear) clear.place(x=250,y=70) ''' def testb(): print (goal) test = Button(master,text = "test", command = testb) test.place (x=100 , y = 200) ''' addm = Label(master, text="Enter New Miles") addm.place (x= 10, y = 10) cur = Label (master, text="Current Miles : "+ str(current)) cur.place(x = 10, y=50) goallab = Label (master,text="Your goal is :" + str(goal) + " Miles") goallab.place(x=10,y=100) left=Label(master,text="Miles Remaining: " + str(rem)) left.place(x=10,y=150) adde = tk.Entry(master) adde.place(x = 100, y = 10) master.mainloop() RE: Part of code is adding extra new line when saving to text file. - Larz60+ - Aug-24-2019 remove line 30 This is a double post which is against forum rules. Please don't do this in the future RE: Part of code is adding extra new line when saving to text file. - lovepeace - Aug-24-2019 I have line 30 in there because if i don't, it combines "goal" and "current" into one number on the same line. sorry for the double post, i didn't know how to delete the first one. as I mentioned, "add miles" and "clear miles" does NOT add the blank line, they all use the same "save" function so i don't know whats going on. RE: Part of code is adding extra new line when saving to text file. - Axel_Erfurt - Aug-24-2019 for me it works (have changed path for Test) also changed some geometry #!/usr/bin/python3 # -*- coding: utf-8 -*- from tkinter import * import tkinter as tk load=open('/tmp/save.txt') current = load.readlines()[0] load.seek(0, 0) goal = load.readlines()[1] left = int(goal) - int(current) load.close() rem = int(goal)-int(current) def clear(): global current current = 0 cur.config(text= "Current Miles: " + str(current)) left.config(text="Miles Remaining " + str(goal)) savefile() return def savefile(): with open("/tmp/save.txt", 'w') as sav: sav.write (str(current)) sav.write("\n") sav.write (str(goal)) sav.close() def addmiles(): global current global rem temp = int(current) add =int(adde.get()) current = (add+temp) rem = int(goal) - int(current) left.config(text= "Miles Remaining: "+ str(rem)) cur.config(text= "Current Miles: " + str(current)) adde.delete(first=0,last=END) savefile() return def setgoal(): global goal goal = int(adde.get()) rem = (goal) - int(current) goallab.config(text= "Your goal is: " + str(goal)+ " Miles.") left.config(text= "Miles Remaining: "+ str(rem)) adde.delete(first=0,last=END) savefile() return master = tk.Tk() master.title("Goal Master v1.0") master.geometry('350x250') add = Button(master, text = "Add", command = addmiles, width=7) add.place(x = 250,y = 10) set= Button(master,text = "Set Goal",command = setgoal, width=7) set.place(x=250,y=40) clear= Button(master,text="Clear Miles",command =clear, width=7) clear.place(x=250,y=70) addm = Label(master, text="Enter New Miles") addm.place (x= 10, y = 10) cur = Label (master, text="Current Miles : "+ str(current)) cur.place(x = 10, y=50) goallab = Label (master,text="Your goal is :" + str(goal) + " Miles") goallab.place(x=10,y=100) left=Label(master,text="Miles Remaining: " + str(rem)) left.place(x=10,y=150) adde = tk.Entry(master, width=10) adde.place(x = 130, y = 10) master.mainloop() RE: Part of code is adding extra new line when saving to text file. - lovepeace - Aug-24-2019 I have no idea why its not working for me. I used the code you edited(changed the path back) and it still does the same thing. I'm so confused. also im using windows 10, ive tried IDLE pycharm and VSC they all do the same thing. :( RE: Part of code is adding extra new line when saving to text file. - Axel_Erfurt - Aug-24-2019 I am on Linux Mint. The text file has 2 lines. cut the line sav.write("\n") RE: Part of code is adding extra new line when saving to text file. - lovepeace - Aug-24-2019 if i cut that line, than add and clear dont work for me. combines them into one number on same line... i pulling my hair out! lol :) RE: Part of code is adding extra new line when saving to text file. - Axel_Erfurt - Aug-24-2019 you can try with open("save.txt", 'w', newline="") as sav: RE: Part of code is adding extra new line when saving to text file. - lovepeace - Aug-24-2019 Still does the same thing. I may have to go about writing a whole new save function and going about it differently, the is the second version of my save function. first one was importing the file at start up, worked fine, but when i compiled it, it wouldn't save to the file. no errors no crashes, just wouldn't update the save file. so i wrote the current one, which is give me my current head ache lol RE: Part of code is adding extra new line when saving to text file. - lovepeace - Aug-24-2019 i fixed it! still not sure why its doing that, but its working now. what i did was convert the int for the clear and add miles back to a str before return, added in a \n with that string. still don't know why they act differently using the same "save" function, but it works now. set goal is returned as a int not a str, and works fine. idk :/ also i compiled it and everything is working still, so yay! Thank you very much for your help! I'm sure I'll have a million more questions lol |