Python Forum

Full Version: how write variable in files and retreive it
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi I m a beginner. Try to learn python by myself. I started to create a program to help me keeping track of information. (kind of excel sheet but in python) I stuck with a problem i spend hours on it but don t figure it out.
This program open a window where you can write a shot number and check or not with a chekerbox. I already manage to create the interface but don t know how to save it on a files and retreive the information. I already start something with txt files but still not working. Try with csv files too , still not working. I include the code it s running. (python2.7) The program dont save variable input and of courses don retreive info when you open the files again . I manage to create a save button but button dont work.I m open to any help. Thanks a lot for your help.
from Tkinter import*

root = Tk()

entry_1 = Entry(root,width= 10)
entry_2 = Entry(root,width= 10)

label_1 = Label (root, width= 10, text = "Shot")
label_2 = Label (root, width= 12, text = "Ready", bg = "pink3")
label_3 = Label (root, width= 12, text = "Wait")

entry_1.grid(row=1)

label_1.grid(row=0, column=0)
label_2.grid(row=0, column=1)
label_3.grid(row=0, column=2)

def var_states():
   SavAll = open("Filestoto3.txt", "wb")
   SavAll.write(var1.get())
   SavAll.close()

var1 = BooleanVar()
Checkbutton(root, variable=var1).grid(row=1, column=1)
var2 = BooleanVar()
Checkbutton(root,  variable=var2).grid(row=1, column=2)


Button(root, text='Quit', command=root.quit).grid(row=11, sticky=W, pady=4)
Button(root, text='Save', command=var_states).grid(row=12, sticky=W, pady=4)
Please use python and output tags when posting code and results. I put them in for you this time. Here are instructions for doing it yourself next time.

The standard way to write to a file is:

with open('data.txt', 'w') as data_file:
    data_file.write(some_string)
The data can be retrieved with

with open('data.txt') as data_file:
    some_string = data_file.read()
You can also read the lines of a file in as a list (data_file.readlines()) or iterate over the lines in a file (for line in data_file:).
Thank you :)