I have created a file which saves the values of 2 sliders. Now, I want to be able to recall values from this file to set the value of the sliders.
This is my current code:
from tkinter import *
import os.path
master= Tk()
master.geometry('500x500+0+0')
def print_value(val):
print ("c1="+str (c1v.get()))
print ("c2="+str(c2v.get()))
c1v=DoubleVar()
c2v=DoubleVar()
c1 = Scale(master, from_=255, to=0, length =400,width =100, troughcolor = 'blue',command=print_value, variable =c1v)
c1.grid(row=1,column=1)
c2 = Scale(master, from_=255, to=0, length =400,width =100, troughcolor = 'blue',command=print_value, variable =c2v)
c2.grid(row=1,column=2)
def record():
save_path = 'C:/Users/Josh Bailey/Desktop/pi_dmx'
name_of_file = ("my first file ")
completeName = os.path.join(save_path, name_of_file+".txt")
file1 = open(completeName , "w")
toFile = ("c1="+str (c1.get())+ "\n""c2="+str(c2.get()))
file1.write(toFile)
file1.close()
master.mainloop()
rec=Button(master, text="Record",width=20, height=10, bg='Red', command=record)
rec.grid(row=2, column=3)
load=Button(master, text="Load",width=20, height=10, bg='gold')
load.grid(row=2, column=4)
You need to make a function to get the data out of the file and then hook up this function to
load
.Following your style, the function would be something like this:
And then you would hook it up to
load
using the button'scommand
option:All in all, the code should be something like this:
Also, just a tip: you should use Python's with-statement when opening files. It auto-closes them for you.