【发布时间】:2015-12-11 11:22:04
【问题描述】:
这是我的代码:
import pickle
from tkinter import *
from tkinter.messagebox import *
Game = Tk()
Gold = 0
Multiply = 1
def Save():
with open('objs.pickle', 'wb') as f:
pickle.dump([Gold, Multiply], f)
def Load():
with open('objs.pickle', 'rb') as f:
return pickle.load(f)
def ClickButton():
global Gold
print(Gold)
Gold +=1 * (Multiply)
Load()
GoldButton = Button(Game, height = 15, width = 25, text="Click!", command ClickButton, bg = "purple")
GoldButton.place(x = 160, y = 95)
save = Button(Game, height = 15, width = 25, text="Click to Save", command = Save)
save.place(x = 380, y = 95)
Game.resizable(width=False, height = False)
Game.geometry('700x450')
Game.title("Gold Farm")
我想要它做什么:
- 从 Pickle 文件中加载变量“Gold”和“Multiply”
- 将新信息保存到关闭后的文件中(Gold 和 Multiply)
问题在于,当我使用文件中的变量重新打开程序时,它只是将 Gold 和 Multiply 设置为默认值
这里有什么问题?我该如何解决?
【问题讨论】: