Python Forum
how write variable in files and retreive it
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how write variable in files and retreive it
#1
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)
Reply
#2
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:).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write a part of powershell command as a variable? ilknurg 2 1,084 Jul-26-2022, 11:31 AM
Last Post: ilknurg
  How do I read the HTML files in a directory and write the content into a CSV file? glittergirl 1 2,556 Sep-23-2019, 11:01 AM
Last Post: Larz60+
  Python Script Spawned by Cron or Systemd doesn't write files..? johnnyrobot 2 2,535 May-24-2019, 07:04 PM
Last Post: Larz60+
  Delete Lines that Contain Words - Loop through files in a folder - Write to new files dj99 3 3,399 May-18-2019, 06:34 AM
Last Post: heiner55
  How to I define a variable between strings in telnetlib write? Fez 2 3,354 May-02-2019, 06:53 PM
Last Post: Fez
  Read and write data to CSV files kartik 1 1,857 Mar-12-2019, 06:16 AM
Last Post: Yoriz
  write each line of a text file into separate text files and save with different names Shameendra 3 2,743 Feb-20-2019, 09:07 AM
Last Post: buran
  How to open and write into several files kazimahmet 3 2,807 May-07-2018, 02:08 PM
Last Post: wavic
  Write Variable String To CSV Cell digitalmatic7 1 6,901 Nov-24-2017, 07:10 AM
Last Post: Prince_Bhatia
  Replacing variable in a split string and write new file python MyCode 1 3,527 Oct-30-2017, 05:20 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020