By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,354 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

How to save the value of a global variable in a file

P: 5
Hi I'm tryin to create a game but I have a question in how to save (saveasfile) the value of a global variable.. and then load the same value with openfile.

Also for example you have a main label and a botton on the left.... so when you click the left bottom the label will change or refresh to another label and when you return to the original label show or refresh to the original one. All the work is with matrices in python 3.2 and also tkinter.

Another question is to check diagonal in the matrix for four 1's diagonal and also antidiagonal.


Thanks!
May 24 '11 #1

✓ answered by bvdet

store_matrix(matrix, filename)
retreive_matrix(filename)

You can assign a function to be called when a button is pressed with the command option.

Share this Question
Share on Google+
9 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Opening a file, writing a value to disc and reading a value from disc are very straightforward. Changing a label can be done with a button command callback. What have you tried so far?
May 24 '11 #2

P: 5
for example I have
Expand|Select|Wrap|Line Numbers
  1. global matriz
  2. matriz=[[0,0,0,0,0,0,0],
  3.         [0,0,0,0,0,0,0],
  4.         [0,0,0,0,0,0,0],
  5.         [0,0,0,0,0,0,0],
  6.         [0,0,0,0,0,0,0],
  7.         [0,0,0,0,0,0,0]]
  8.  
  9.  
  10. def savematrix():
  11.     filedialog.asksaveasfile ()
so I want to save the global matrix variable on a file maybe txt.. and then when I use the

Expand|Select|Wrap|Line Numbers
  1. def loadmatrix():
  2.     filedialog.askopenfilename() 
load the matrix and give the global value the one that was save on the txt.

Thanks for the reply
May 24 '11 #3

100+
P: 332
Look at the shelve module in the documentation. It allows easy disk storage/retrieval of objects.
May 24 '11 #4

bvdet
Expert Mod 2.5K+
P: 2,851
Mariostg has given good advice. You could also serialize and deserialize your data with the pickle module. You could also do something like this:
Expand|Select|Wrap|Line Numbers
  1. def store_matrix(m, fn):
  2.     f = open(fn, 'w')
  3.     output = []
  4.     for row in m:
  5.         output.append(",".join([str(item) for item in row]))
  6.     f.write("\n".join(output))
  7.     f.close()
  8.     return True
  9.  
  10. def retreive_matrix(fn, objType):
  11.     f = open(fn)
  12.     m = []
  13.     for line in f:
  14.         m.append([objType(item) for item in line.strip().split(",")])
  15.     f.close()
  16.     return m
  17.  
  18. m = [[1,2,3],[4,5,6],[7,8,9]]
  19.  
  20. store_matrix(m, "test.txt")
  21.  
  22. print retreive_matrix("test.txt", int)
May 24 '11 #5

P: 5
can you please explain me the arguments of each function... and does this function can be implement into tkinter buttom
Thanks for the reply.. really appreciate it.
May 24 '11 #6

bvdet
Expert Mod 2.5K+
P: 2,851
store_matrix(matrix, filename)
retreive_matrix(filename)

You can assign a function to be called when a button is pressed with the command option.
May 24 '11 #7

P: 5
Expand|Select|Wrap|Line Numbers
  1.     def savematrix():
  2.         global matrix
  3.         global player1
  4.         global player2
  5.         global booleanx
  6.         try:
  7.             fn=nombretexto.get(0.0,END)
  8.             fn=eval(fn)
  9.             print(fn)
  10.             f = open(fn, 'w')
  11.             output = []
  12.             for row in matriz:
  13.                 output.append(",".join([str(item) for item in row]))
  14.             f.write("\n".join(output))
  15.             f.close()
  16.  
  17.     def loadmatrix():
  18.         global matrix
  19.         global player
  20.         global player2
  21.         global booleanx
  22.         try:
  23.             nombre=nombretexto2.get(0.0,END)
  24.             nombre=eval(nombre)
  25.             f = open(nombre)
  26.             m = []
  27.             for line in f:
  28.                 m.append([int(item) for item in line.strip().split(",")])           
  29.             matriz=m
  30.             f.close()
  31.  
  32.     player1="player1"
  33.     player2="player2"
  34.     booleanx=False

Hi I have these two functions!!! At the moment it save a matrix and is able to load it and at the same time make the global matrix = to the one it was saved. I need to save also the player1 player2 and booleanx also.. so when I loaded... I can get the global player1 player2 and booleanx to the value it was save.. Thanks!!
May 26 '11 #8

bvdet
Expert Mod 2.5K+
P: 2,851
Write the values to the same file before the matrix. Assign the first line of the file to the variables. Continue reading the file for the matrix.

Untested:
Expand|Select|Wrap|Line Numbers
  1. # opened file object for writing
  2. f.write(",".join([player1, player2, str(booleanx)]))
  3.  
  4. # opened file object for reading
  5. player1, player2, eval(booleanx) = f.readline().strip().split(",")
  6. for line in f:
  7.     ..................
May 27 '11 #9

P: 5
Dude sorry I'm a newbie but I don't know where to put them inside the code that I already have... Also it says something about calling a function error because of this
Expand|Select|Wrap|Line Numbers
  1. player1, player2, eval(booleanx) = f.readline().strip().split(",")
If you please can tell me where to add the code..
Then I'll just need to put booleanx= booleanx and so on.. THANKS for responding!!
May 27 '11 #10

Post your reply

Sign in to post your reply or Sign up for a free account.