Python Forum
Part of code is adding extra new line when saving to text file.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Part of code is adding extra new line when saving to text file.
#1
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()
Reply


Messages In This Thread
Part of code is adding extra new line when saving to text file. - by lovepeace - Aug-23-2019, 11:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] Saving file to html or pdf stopped working in PyQt6 ejKDE 4 625 Mar-12-2024, 07:45 PM
Last Post: ejKDE
  [PyQt] Adding a new file/node to QFileSystemModel DrakeSoft 1 1,094 Feb-06-2023, 01:51 PM
Last Post: deanhystad
  [PyQt] [Solved]Change text color of one line in TextBrowser Extra 2 4,977 Aug-23-2022, 09:11 PM
Last Post: Extra
  [Tkinter] logical error - saving file rwahdan 4 2,175 Jul-01-2021, 12:59 PM
Last Post: rwahdan
  Line numbers in Text widget rfresh737 3 5,508 Apr-15-2021, 12:30 PM
Last Post: rfresh737
  GUI skips code part MLGpotato 7 3,617 Feb-28-2021, 11:22 AM
Last Post: deanhystad
  [PyGTK] How to center text on multi-line buttons? Lomax 3 4,297 Jan-23-2021, 03:23 PM
Last Post: Lomax
  [gtk] How to modify a part of a text of a gtk textview cyrille 1 2,345 Nov-24-2020, 06:31 PM
Last Post: cyrille
  [PyQt] saving text file by FileDialog option atlass218 14 4,734 Feb-19-2020, 09:22 AM
Last Post: atlass218
  [Tkinter] Line Number Tkinter Text Are smabubakkar 1 4,405 Jun-22-2019, 07:05 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020