我无法保存列表

时间:2019-11-17 20:16:29

标签: python-3.x save

import tkinter as tk

list = []

def add():
    element = entry.get()
    entry.delete(0, 1000)
    list.append(element)

def save():
    with open("save.txt", "w") as f:
        for element in list:
            f.write(element + ",")

root = tk.Tk()

frameList = tk.Frame(root)
frameList.pack()

entry = tk.Entry(root)
entry.pack()

btnAdd = tk.Button(root, text="add", command=add)
btnAdd.pack(pady=5)

btnSave = tk.Button(root, text="save", command=save)
btnSave.pack()

root.mainloop()

此代码在保存时给我这个错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/Users/HP/Desktop/python/github/write.py", line 11, in save
    with open("save.txt", "w") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'save.txt'

如果我创建文件“ save.txt”,则会显示:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/Users/HP/Desktop/python/github/write.py", line 13, in save
    f.write(element + ",")
OSError: [Errno 9] Bad file descriptor

请,您能帮我解决这个问题吗?

对不起,最后一句话,但是我必须添加一些“非代码”来发布问题,所以从现在开始,我会说一些随机的东西,对不起

0 个答案:

没有答案