',python,tkinter,attributeerror"/>

首页 > 解决方案 > Cookie Clicker 游戏错误“无法获取属性“gm”'

问题描述

我正在尝试制作一个 cookie clicker 游戏,一切正常,但现在出现错误

new_dict=pickle.load(infile)
AttributeError: Can't get attribute 'gm' on <module '__main__' (built-in)>
import tkinter as tk
from tkinter import *
import pickle

#save data transfer

filename='cookie_clicker'
infile=open(filename,'rb')
new_dict=pickle.load(infile)
infile.close()
print(new_dict)

cookies = new_dict.get('cookies')
click_amount=new_dict.get('click_amount')
gm= new_dict.get('grandmas')
gm_cost=new_dict.get('gm_cost')

root = Tk()
root.title("Cookie Clicker")


#button commands

def click():
    global cookies,click_amount
    cookies += click_amount
    cookies_Label.config(text='cookies you have: '+str(cookies))
    cookies_dict={'cookies':cookies,
                  'click_amount':1,
                  'grandmas':gm,
                  'gm_cost': 50}
    outfile=open(filename,'wb')
    pickle.dump(cookies_dict,outfile)
    outfile.close()
    
def buy_grandma():
    global cookies,gm
    if cookies>= gm_cost:
        gm +=1
        cookies -= gm_cost
        cookies_Label.config(text='cookies you have: '+str(cookies))
        cookies_dict={'cookies':cookies,
                  'click_amount':1,
                  'grandmas':gm,
                  'gm_cost': 50}
        outfile=open(filename,'wb')
        pickle.dump(cookies_dict,outfile)
        outfile.close()

def quit():
    root.destroy()

    
#click section

frame=Frame(master=root,bg='#F0FFFC',width =400, height=400)
frame.pack(side=TOP,fill=tk.BOTH)
        
cookies_Label =Label(frame,text = 'cookies you have: ' + str(cookies),relief='groove',borderwidth= 3)
cookies_Label.grid(row = 0, column=1,columnspan=3, padx=110, pady=10)

cookie_button= Button(frame,padx = 110,pady = 100,command = click)
cookie_button.grid(row=1, column=0,columnspan=4)

exit_button = Button(frame,text = 'X',relief='groove',borderwidth=3, command=quit)
exit_button.grid(row=0,column=0)


#upgrade section

up_frame=Frame(root,bg= '#FFF1E4',relief='groove',borderwidth=3,width=400, height=400)
up_frame.pack(side=BOTTOM,fill=tk.BOTH)

clicker_up=Frame(up_frame,text='''clicker upgrade
+1 cookie per click
costs : '''+str(clicker_up_cost)+'cookies',relief='groove',borderwidth=3)
clicker_up.grid(row=0, column=0)


gm_label = Label(up_frame,text='''grandma
+1 cookies per second''',relief='groove', borderwidth=3)
gm_label.grid(row=1, column=0)

gm_button = Button(up_frame,text= '''upgrade grandma :
 '''+ str(gm_cost)+' cookies',relief='groove',borderwidth=3,command=buy_grandma,state=NORMAL)
gm_button.grid(row=1,column=1)

#autoclicker


root.mainloop()

我不知道要发送代码的哪一部分,所以我全部发送了。

请帮助它将不胜感激。

标签: pythontkinterattributeerror

解决方案


推荐阅读