简体   繁体   English

用“ w”打开file.open不会在Python tKinter按钮方法中覆盖文件

[英]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. 因此,我正在为该项目编写tKinter GUI,但我遇到了一个按钮方法问题。 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. 第一次效果很好,但是如果我在关闭根tKinter窗口之前再次按下按钮,它不会截断文件-它只是将下一个起始坐标添加到末尾。 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. 现在,这是在button方法中,因此据我所知,它应该在每次按下按钮时执行。 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. 在wtf函数中,如果'w'是标志(reportFile = open('gridCenters.txt','w')),则每次都重写gridCenters.txt,但是如果标志是'a'而不是'w',则结果为只是追加一个。我希望这就是你想要的。

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. 您可以在文件关闭后重置它,除非此时需要保留它以供GUI使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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