简体   繁体   English

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

[英]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.

相关问题 Tkinter:如何使用 scale-widget 的值来更改 label-widget 的文本? - Tkinter: How do I use the value of a scale-widget to change the text of a label-widget? 如何从 tkinter Scale 小部件显示中删除比例值? - How to remove scale value from tkinter Scale widget display? 如何在 tkinter 的文本小部件中设置值? - how to set value in Text widget in tkinter? 如何在Python中为tkinter Scale小部件设置初始值? - How set the initial value for a tkinter Scale widget in Python? 如何将Tkinter Scale小部件的状态设置为Disabled? - How to set the state of a tkinter Scale widget to disabled? 如何打印tkinter Scale小部件的值? - How to print the value of a tkinter Scale widget? 如何使用 tkinter 中的按钮设置“Entry”小部件的文本/值/内容 - How to set the text/value/content of an `Entry` widget using a button in tkinter Python/Tkinter:如何将文本小部件内容设置为变量的值? - Python/Tkinter: How to set text widget contents to the value of a variable? 如何根据Python中另一个tkinter`Scale`小部件的值来控制tkinter`Scale`小部件的值? - How control the value of a tkinter `Scale` widget depending on the value of another tkinter `Scale` widget in Python? 如何将 tkinter Scale 小部件滑块的默认值设置为 100? - How can I set the default value of my tkinter Scale widget slider to 100?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM