【发布时间】:2017-03-02 03:19:22
【问题描述】:
我做了简单的提交分数选项。用户输入姓名和分数,并将它们保存到列表中。列表仅保存前 10 个分数。比,列表打印到 .txt 文件。但它仅在程序运行时才有效。当我再次启动它时,.txt 文件中只有默认分数。未保存用户分数。为此,我正在使用 pickle 模块。
这是我的一段代码。它是 Python 3.4 和 Tkinter。 请记住,我正在学习 python。
# This is inside class
# ...
# ...
self.printto = tk.Button(self, text="Submit",
command=self.highscore
)
self.printto.pack(side="left")
self.high_scores = [
('Liz', 1800)
]
def highscore(self):
name = self.name_ent.get()
score = int(self.score_ent.get())
self.high_scores.append((name, score))
high_scores = sorted(self.high_scores, key=itemgetter(1), reverse=True)[:10]
with open('D:\Desktop/mytext.txt', 'wb') as f:
pickle.dump(high_scores, f)
【问题讨论】:
标签: python windows list tkinter pickle