简体   繁体   中英

How to use value from a text file with the `set` method of a tkinter Scale widget?

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 can set the value of the slider like so:

c1.set(100) #sets value to 100

So just read a text file and replace 100 in my example with the values from the file:

the code:

try:
    f = open("my first file.txt")
    for each_line in f:
        (slider, value) = each_line.split("=")
        if slider == "c1":
            saved_c1 = value
        if slider == "c2":
            saved_c2 = value
except FileNotFoundError:
    pass

c1.set(saved_c1)
c2.set(saved_c2)

You also have a trailing space after your file name(my first file). It would probably be best to get rid of that.

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:

def func():

    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)
    val1, val2 = (x.split("=")[1] for x in file1)
    c1v.set(val1)
    c2v.set(val2)
    file1.close()

And then you would hook it up to load using the button's command option:

load=Button(master, text="Load",width=20, height=10, bg='gold', command=func)
load.grid(row=2, column=4)

All in all, the code should be something like this:

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)

#################################################################
def func():

    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)
    val1, val2 = (x.split("=")[1] for x in file1)
    c1v.set(val1)
    c2v.set(val2)
    file1.close()

load=Button(master, text="Load",width=20, height=10, bg='gold', command=func)
load.grid(row=2, column=4)
###################################################################
record()

Also, just a tip: you should use Python's with-statement when opening files. It auto-closes them for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM