简体   繁体   中英

file.open with “w” not overwriting file in Python tKinter button method

So I'm writing a tKinter GUI for this project I'm working on, and I've run into a problem with one of my button methods. In the method for this button, the code prints a list of coordinates to a text file. It works great the first time, but if I press the button again before closing the root tKinter window, it doesn't truncate the file - it just adds the next set off coordinates to the end. Here is my code:

#print to file
reportFile = open('gridCenters.txt','w')
reportFile.write('In movement order:\n')
for x in xrange(0,len(coordinates)):
    reportFile.write('%s\n' % str(coordinates[x]))
reportFile.close()

Now, this is within a button method, so to my understanding it should execute every time the button is pressed. The really strange part is that in the output after pressing the button again, it prints JUST the loop values. For some reason it skips over the "In movement order" part. It won't let me upload images but here's an idea of how it looks:

In movement order:
(0,1)
(0,2.5)
(0.3.5)
(0,4.5)

Then if I press the button again before closing the root window:

In movement order:
(0,1)
(0,2.5)
(0.3.5)
(0,4.5)
(0,1)
(0,2.5)
(0.3.5)
(0,4.5)

(Those blocks aren't code, just text output)

I'm just really confused. My understanding is that every time I press the button, it should overwrite the file, then close it.

Thanks for the help.

I am not shure why it doesnt works for you but here is what i had wrote.

from Tkinter import *

def wtf(coordinates):
    reportFile = open('gridCenters.txt','w')
    reportFile.write('In movement order:\n')
    for x in xrange(0,len(coordinates)):
        reportFile.write('%s\n' % str(coordinates[x]))
    reportFile.close()

def main():
    coordinates = [(0,1),(0,2.5),(0,3.5),(0,4.5)]
    root = Tk()
    btn = Button(root,text='click me',command = lambda:wtf(coordinates))
    btn.pack()

    root.mainloop()
main()

in wtf function if 'w' is flag (reportFile = open('gridCenters.txt','w')) the gridCenters.txt is rewritten every time,but if the flag is 'a' instead of 'w' than result is just appending one below another.I hope this is what u want.

from Tkinter import *
coords = [1, 2, 3, 4, 5]
def write():
    global coords
    fileName = "testButton.txt"
    fileObj = open(fileName, 'w')
    fileObj.write("Some words\n")
    for i in xrange(0, len(coords)):
        fileObj.write("%d\n" %coords[i])
    fileObj.close()
    for i in range(5):
        coords[i] += 1

root = Tk()
f = Frame(root).pack()
b = Button(root, text = "OK", command = write).pack(side = LEFT)

root.mainloop()

This works for me, overwriting the file every time and the values are updated every time as well. Something must be going on elsewhere in your program.

In when your button re-opens the file it doesn't print the "In movement order:" a second time.

This looks like you aren't clearing your variable coordinates. You should make sure that you are starting with a clean variable before adding to it to get the data you are looking for.

You could reset it after the file closes unless you need to retain it for use un the GUI at that point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM