简体   繁体   中英

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

I am trying to save and load some values and names as a small proyect but I have some troubles with the load part I am using PYTHON 3.4 and tkinter to create 4 texboxes, 2 are for names and 2 are for values and 2 buttons to save and load what I put on these textboxes I can write in these textboxes anything, so lets say I put

apple 20

orange 40

so in my first 2 textboxes I have 2 Strings and Integers in the other 2
so my code is

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)

save is working and I can create an archive .txt that shows

apple,20,orange,40

but when I try to put these values in the textboxes, I can't python says

IndexError: list index out of range

I just want, when the four textboxes are empty and I press the button Load, to put apple in textbox 1, 20 in textbox 2, orange in textbox 3 and 40 in textbox 4 again

what should I do? any help please

EDITED

This is the final code, thanks

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() only gives you a path to a file. It does not actually read a file. Thus you need to open it and read. Also, if you have only one line in a file, as in your example, this (x.split(",")[3] for x in filenamel) wont work, as it iterates over a letters in the filepaths, not lines in the file. You should do as follows in mLoad() instead:

# 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(",")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM