Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在Tkinter GUI中,是否有一种更具Python风格的方法将列表中的值分配给多个StringVar?_Python 3.x_List_Tkinter - Fatal编程技术网

Python 3.x 在Tkinter GUI中,是否有一种更具Python风格的方法将列表中的值分配给多个StringVar?

Python 3.x 在Tkinter GUI中,是否有一种更具Python风格的方法将列表中的值分配给多个StringVar?,python-3.x,list,tkinter,Python 3.x,List,Tkinter,您好,我正在努力寻找一种更干净的方法来处理文本文件导入中的多个StringVar分配。随着项目的发展,我最终使用的任何方法都需要适应未来更多的StringVar 文本文件导入中的每一行在值“key:value”前面都有一个键,其中分隔符为“:”。虽然我目前有文本文件行的顺序固定,这将不会是在这个项目的未来情况。我正在进行的某些设置可能会影响此文本文件中存储的值,如果没有相关数据,则可能不会写入某些键 据我所知,我需要StringVar来填充动态tkitner GUI标签和条目元素,所以我看不到使

您好,我正在努力寻找一种更干净的方法来处理文本文件导入中的多个StringVar分配。随着项目的发展,我最终使用的任何方法都需要适应未来更多的StringVar

文本文件导入中的每一行在值“key:value”前面都有一个键,其中分隔符为“:”。虽然我目前有文本文件行的顺序固定,这将不会是在这个项目的未来情况。我正在进行的某些设置可能会影响此文本文件中存储的值,如果没有相关数据,则可能不会写入某些键

据我所知,我需要StringVar来填充动态tkitner GUI标签和条目元素,所以我看不到使用StringVar的方法。下面是我的保存文件导入函数的一个片段,该函数从输入文件数据中指定StringVar值

我目前的解决方案是手动将每个StringVar设置为列表中的一个值,这有点笨拙,但比第一次使用的大量if-else语句评估标签要好

    # Read in the data
    raw = open_regatta_save_file(file_path)

    # Process the regatta save file.
    row = 0   

    # Process Regatta Information
    regatta_labels = ["Regatta_Name", "Regatta_Host", "Regatta_Location", "Regatta_Start_Day",
                      "Regatta_Start_Month", "Regatta_Start_Year", "Regatta_End_Day", "Regatta_End_Month",
                      "Regatta_End_Year", "Regatta_Type", "Regatta_Throw_Outs"]
    regatta_values = [""] * len(regatta_labels)
    while raw[row] != ":::BOAT INFORMATION:::":  # Header for next section of input file
        line = raw[row]
        if ': ' in line and line.startswith(':::') is False:
            # This is a line with data in it and not a header
            [label, data] = line.split(': ')
            if data is None or data == '':
                # Either the provided data was blank or missing
                continue
            else:
                if label in regatta_labels:
                    label_index = regatta_labels.index(label)
                    regatta_values[label_index] = data
        row += 1

    # Set regatta parameter values. Since regatta_labels control the order of the regatta_values list,
    # these variables can be assigned in order.
    self.regatta_name.set(regatta_values[0])
    self.regatta_host.set(regatta_values[1])
    self.regatta_location.set(regatta_values[2])
    self.regatta_start_day.set(regatta_values[3])
    self.regatta_start_month.set(regatta_values[4])
    self.regatta_start_year.set(regatta_values[5])
    self.regatta_end_day.set(regatta_values[6])
    self.regatta_end_month.set(regatta_values[7])
    self.regatta_end_year.set(regatta_values[8])

    # Process Rest of file using similar method.
我希望有人能帮助我更好地理解Python StringVar,这样我就可以做一些更简单的事情,比如:

StringVar1, StringVar2 ... = [list with values to assign to StringVars]

谢谢您的帮助。

尝试使用List/Dictionary of StringVars(),然后您可以使用循环来设置或获取值。您的意思是在while循环中创建临时StringVars并填充列表,然后只是重写tkinter StringVars?是的,用循环填充它,然后用循环覆盖它