How can I fix this error in python 3.4 tkinter "Index Error: list index out of range"

How can I fix this error in python 3.4 tkinter "Index Error: list index out of range"

我正在尝试将一些值和名称保存和加载为一个小项目,但我在加载部分遇到了一些问题 我正在使用 PYTHON 3.4 和 tkinter 创建 4 个文本框,2 个用于名称,2 个用于值,2 个按钮用于保存和加载我放在这些文本框中的内容 我可以在这些文本框中写任何东西,所以假设我输入

苹果 20

橙40

所以在我的前 2 个文本框中我有 2 个字符串和其他 2 个整数
所以我的代码是

import time
import serial
import sys
import os
import tkinter as tk
from tkinter import ttk
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
from tkinter.filedialog import askopenfilename
from tkinter.filedialog import asksaveasfilename
from tkinter.messagebox import showerror
try:
  import Tkinter              # Python 2
  import ttk
except ImportError:
  import tkinter as Tkinter   # Python 3
  import tkinter.ttk as ttk
mGui = Tk()
mGui.title("trying")
mGui.geometry('1250x650+10+10')
def mSave():
  filename = asksaveasfilename(defaultextension='.txt',filetypes = (('Text files', '*.txt'),    ('Python files', '*.py *.pyw'),('All files', '*.*')))
  if filename is None:
    return
  file = open (filename, mode = 'w')
  NameVal_1 = name1.get()
  NameVal_2 = name2.get()
  Vol_Val_1 = value1.get()
  Vol_Val_2 = value2.get()

  all =   (NameVal_1 + "," + (str(Vol_Val_1)) + ","
         + NameVal_2 + "," + (str(Vol_Val_2)))
  file.write(all)
  file.close()

def mLoad():
  filenamel = askopenfilename()
  if filenamel is None:
    return
  (NameVal_1, Vol_Val_1,
   NameVal_2, Vol_Val_2)   = (x.split(",")[3] for x in filenamel)

  name1.set(NameVal_1)
  name2.set(NameVal_2)
  value1.set(Vol_Val_1)
  value2.set(Vol_Val_2)
  file.close()

value1 = IntVar()
value2 = IntVar()
name1 = StringVar()
name2 = StringVar()

mButtonSave = Button(mGui, text = "Save Data", command = mSave, fg = 'Red').place(x=550,y=80)
mButtonLoad = Button(mGui, text = "Load Data", command = mLoad, fg = 'Red').place(x=550,y=110)

tText1 = Entry(mGui, textvariable = name1).place(x=10,y=80)
tText2 = Entry(mGui, textvariable = name2).place(x=10,y=100)
vText1 = Entry(mGui, textvariable = value1).place(x=200,y=80)
vText2 = Entry(mGui, textvariable = value2).place(x=200,y=100)

正在保存,我可以创建一个存档 .txt 来显示

apple,20,orange,40

但是当我尝试将这些值放入文本框时,我做不到 python 说

IndexError: list index out of range

我只是想,当四个文本框为空时,我按下加载按钮,将苹果放入文本框 1,将 20 放入文本框 2,将橙色放入文本框 3,将 40 放入文本框 4

我该怎么办?请帮忙

已编辑

这是最终代码,谢谢

import time
import serial
import sys
import os
import tkinter as tk
from tkinter import ttk
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
from tkinter.filedialog import askopenfilename
from tkinter.filedialog import asksaveasfilename
from tkinter.messagebox import showerror
try:
  import Tkinter              # Python 2
  import ttk
except ImportError:
  import tkinter as Tkinter   # Python 3
  import tkinter.ttk as ttk
mGui = Tk()
mGui.title("trying")
mGui.geometry('1250x650+10+10')
def mSave():
  filename = asksaveasfilename(defaultextension='.txt',filetypes = (('Text files', '*.txt'),    ('Python files', '*.py *.pyw'),('All files', '*.*')))
  if filename is None:
    return
  file = open (filename, mode = 'w')
  NameVal_1 = name1.get()
  NameVal_2 = name2.get()
  Vol_Val_1 = value1.get()
  Vol_Val_2 = value2.get()

  all =   (NameVal_1 + "," + (str(Vol_Val_1)) + ","
         + NameVal_2 + "," + (str(Vol_Val_2)))
  file.write(all)
  file.close()

def mLoad():
  filenamel = askopenfilename()
  if filenamel is None:
    return
  with open(filenamel, 'r') as f:
    x = f.readline()  # read the first line
    (NameVal_1, Vol_Val_1,  NameVal_2, Vol_Val_2) = x.split(",")  

  name1.set(NameVal_1)
  name2.set(NameVal_2)
  value1.set(Vol_Val_1)
  value2.set(Vol_Val_2)
  filename.close()

value1 = IntVar()
value2 = IntVar()
name1 = StringVar()
name2 = StringVar()

mButtonSave = Button(mGui, text = "Save Data", command = mSave, fg = 'Red').place(x=550,y=80)
mButtonLoad = Button(mGui, text = "Load Data", command = mLoad, fg = 'Red').place(x=550,y=110)

tText1 = Entry(mGui, textvariable = name1).place(x=10,y=80)
tText2 = Entry(mGui, textvariable = name2).place(x=10,y=100)
vText1 = Entry(mGui, textvariable = value1).place(x=200,y=80)
vText2 = Entry(mGui, textvariable = value2).place(x=200,y=100)

filenamel = askopenfilename() 只给你一个文件的路径。它实际上并不读取文件。因此,您需要打开它并阅读。此外,如果文件中只有一行,如示例所示,此 (x.split(",")[3] for x in filenamel) 将不起作用,因为它遍历文件路径中的字母,而不是文件中的行。您应该在 mLoad() 中执行以下操作:

# open the file for reading
with open(filenamel, 'r') as f:
    x = f.readline()  # read the first line   

# split it by ',' and assing to appropriate variables.
(NameVal_1, Vol_Val_1,  NameVal_2, Vol_Val_2) = x.split(",")