0

I was trying to make a cookie clicker game and all was progressing normally but now there is an error

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()

I don't know which part of the code to send so I sent it all.

Please help it would be very appreciated.

TheLizzard
  • 7,248
  • 2
  • 11
  • 31
  • I think that `cookie_clicker` is supposed to be a pickle file but how did you create it? Is it possible to send it to us? – TheLizzard Mar 05 '21 at 17:08
  • Post the full error code please. – Delrius Euphoria Mar 05 '21 at 17:13
  • Does this answer your question? [Unable to load files using pickle and multiple modules](https://stackoverflow.com/questions/27732354/unable-to-load-files-using-pickle-and-multiple-modules) – Carcigenicate Mar 05 '21 at 17:17
  • The problem seems to be that you need to `load` the file from the same class that you `dump`ed it from (if I'm interpreting their answer correctly). `load` relies on the enclosing class to pull information from. – Carcigenicate Mar 05 '21 at 17:18
  • @TheLizzard im not entirely sure how i made the pickle file but ```import pickle cookies_dict={'cookies':10, 'click_amount':1, 'grandmas':0} filename='cookie_clicker' outfile=open(filename,'wb') pickle.dump(cookies_dict,outfile) outfile.close() infile=open(filename,'rb') new_dict=pickle.load(infile) infile.close() print(new_dict) x = new_dict.get('cookies') print(x) print(new_dict) ``` –  Mar 05 '21 at 17:52
  • @CoolCloud ``` Traceback (most recent call last): File "/Users/Amin/Desktop/Cookie Clicker/Cookie Clicker fully functioning.py", line 6, in infile=open(filename,'rb') FileNotFoundError: [Errno 2] No such file or directory: 'cookie_clicker' ``` –  Mar 05 '21 at 17:55
  • It can't find the file `cookie_clicker`. Are you sure it is in the same dir as your python script? – TheLizzard Mar 05 '21 at 17:56
  • @TheLizzard it was working completely fine till i tried to add a auto clicker but i got rid of all the changes and it still doesnt work –  Mar 05 '21 at 17:59
  • @Carcigenicate i took a look at the answers on that question but i really don't understand it –  Mar 05 '21 at 17:59
  • @AminAli did you ever create a class called `gm`? – TheLizzard Mar 05 '21 at 18:01
  • @TheLizzard i didn't make any classes called gm it was just a variable but i don't really know what a class is –  Mar 05 '21 at 18:05
  • Have you tried to reset the file using the code above? I think that something is wrong with your file right now and it is expecting a class/variable named `gm`. – TheLizzard Mar 05 '21 at 18:11
  • @TheLizzard i tried to reset the file but i forgot to save the original one just in case and it turns out it didn't work so now im trying to fix it up which will probably take a while. –  Mar 05 '21 at 18:24

0 Answers0