[英]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. 我创建了一个保存2个滑块值的文件。 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: 因此,只需读取一个文本文件,然后将示例中的100替换为文件中的值即可:
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
. 您需要创建一个函数以从文件中获取数据,然后将其连接以进行
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: 然后使用按钮的
command
选项将其挂接以load
:
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. 另外,提示:打开文件时,应使用Python的with语句 。 It auto-closes them for you.
它会自动为您关闭它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.