Home > Enterprise >  Writing variables from GUI to text file in tkinter
Writing variables from GUI to text file in tkinter

Time:09-29

I'm trying to write the text fields values to a file like this (where inputtxt is a Text widget):

    input_file_name =   inputtxt1.get('1.0', 'end-1c').replace(" ","")
    num_of_compare_points = inputtxt2.get('1.0','end-1c')
    VER = inputtxt3.get('1.0','end-1c')
    mode = inputtxt4.get(1.0','end-1c')
    executionMode = inputtxt5.get('1.0','end-1c')
    numOfWorkers = inputtxt6.get ('1.0','end-1c')
    sessionFile = inputtxt7.get('1.0','end-1c')

    file = open("file_1.setup","w")
         file.write (input_file_name   "\n"   num_of_compare_points   "\n"      Option "\n" VER "\n" mode "\n" executionMode "\n" numOfWorkers "\n" sessionFile)

This method was good but I want to write the variable name the value to the file so that the user can fill the values from the file not only through GUI and the order in file will be irrelevant since we're storing in the variable itself , Example: File_1.setup: input_file_name = (some name the user can change it from here not from GUI) num_of_compare_points = (some number the user can change it from here not from GUI) . . . etc UPDATE: I'm using the file attribute:value in a restore finction which put the value of each attribute on its related text field ; the problem here that I have only the value but not the variable:value and the variables must be in order (I need the order to be irrelevent since I'll use the variable name)

def restore_info():

if os.stat('file_1.setup').st_size == 0:
    print("Writing to setup file..")
else:
    with open('file_1.setup','r') as f:
    
            lines = list(line for line in (l.strip() for l in f) if line)
    x = len(lines)
    print (x)
    for i in lines:
        print (i)                   
    if (x==7):
        inputtxt1.insert('end',lines[0])
        inputtxt2.insert('end',lines[1])
        inputtxt3.insert['end',lines[2])
        ... etc

UPDATE2 : I've managed to split each line based on ":" but I need a way to tell the program where is variable and where is value

def restore_info():

if os.stat('file_1.setup').st_size == 0:
    print("Writing to setup file..")
else:
    with open('file_1.setup','r') as f:
    
            lines = list(line for line in (l.strip() for l in f) if line)
    x = len(lines)
    print (x)
    for i in lines:
        splitted_i=i.split(":")
        print (splitted_i)

UPDATE 3:Advantage of this step

  1. User will be able to apply the value manually, and not only through GUI.
  2. The order will be irrelevant.
  3. Any amount of spaces should be allowed around and in between the variable name and its value.
  4. It will be very easy to search file_1.setup and extract the value for each field.

CodePudding user response:

What you need to do is to collect the data into a dict and dump it as key value pair

sessionFile = 'x'
executionMode = 'y'
data = {'sessionFile': sessionFile, 'executionMode': executionMode}
with open('out.txt', 'w') as f:
    for k, v in data.items():
        f.write(f'{k}:{v}\n')

out.txt

sessionFile:x
executionMode:y
  • Related