简体   繁体   English

如何在python 3.4 tkinter中修复此错误“索引错误:列表索引超出范围”

[英]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 我正在尝试将一些值和名称保存并加载为小对象,但是在加载部分时遇到了一些麻烦,我正在使用PYTHON 3.4和tkinter创建4个texbox,2个用于名称,2个用于值,以及2个要保存的按钮并加载我在这些文本框上输入的内容,然后我可以在这些文本框中写任何内容,所以可以说我把

apple 20 苹果20

orange 40 橙色40

so in my first 2 textboxes I have 2 Strings and Integers in the other 2 所以在我的前两个文本框中,我在其他两个文本框中有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 保存正在工作,我可以创建一个显示以下内容的存档.txt

apple,20,orange,40

but when I try to put these values in the textboxes, I can't python says 但是当我尝试将这些值放在文本框中时,我无法使用python说

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 我只想在四个文本框为空并且按下“加载”按钮时,将苹果放在文本框1中,将20放在文本框2中,将橙色放在文本框3中,再将40放在文本框4中。

what should I do? 我该怎么办? any help please 任何帮助请

EDITED 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. filenamel = askopenfilename()仅为您提供文件的路径。 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. 另外,如果文件中只有一行,如您的示例,则此(x.split(",")[3] for x in filenamel) ,因为它遍历(x.split(",")[3] for x in filenamel)路径中的字母而不是行在文件中。 You should do as follows in mLoad() instead: 您应该在mLoad()执行以下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(",")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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