1
這是我的代碼:保存數據與泡菜和Tkinter的供以後使用
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")
我想要它做什麼:
- 負荷泡椒變量「黃金」和「乘法」文件
- 保存新的信息文件APON關閉(黃金及乘)
它的問題是,當我重新打開該文件中的變量的程序,它只是將Gold和Multiply設置爲它們的默認值,不管
這裏有什麼問題?我該如何解決?
謝謝,這個作品。 :)但我有另外一個問題,當文件尚未存在,或文件爲空時,它會出現錯誤,我將如何防止這種情況發生? – Csarg
@Csarg 'os.path.isfile(file)' – SmartManoj