如何使用tkinter Scale小部件的`set`方法从文本文件中使用值?

时间:2013-12-27 17:00:14

标签: python tkinter save load tkinter-scale

我创建了一个保存2个滑块值的文件。现在,我希望能够从该文件中调用值来设置滑块的值。

这是我目前的代码:

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)

2 个答案:

答案 0 :(得分:1)

您可以像这样设置滑块的值:

c1.set(100) #sets value to 100

因此,只需阅读一个文本文件,并在我的示例中将100替换为文件中的值:

代码:

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)

您的文件名(我的第一个文件)后面还有一个尾随空格。最好摆脱它。

答案 1 :(得分:0)

您需要创建一个函数来从文件中获取数据,然后将此函数连接到load

按照你的风格,功能将是这样的:

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选项将其连接到command

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

总而言之,代码应该是这样的:

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()

另外,只是一个提示:打开文件时应该使用Python的with-statement。它会为你自动关闭它们。