我正在尝试保存并加载一些值和名称作为一个小项目,但我有一些负载部分的麻烦 我使用PYTHON 3.4和tkinter创建4个texbox,2个用于名称,2个用于值,2个按钮用于保存和加载我放在这些文本框中的内容 我可以在这些文本框中写任何东西,所以我说我放
apple 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)
保存工作正常,我可以创建一个显示
的存档.txtapple,20,orange,40
但是当我尝试将这些值放在文本框中时,我不能 python说
IndexError: list index out of range
我只想要,当四个文本框为空时我按下按钮Load,将苹果放入文本框1,文本框2中的20,文本框3中的橙色和文本框4中的40再次
我该怎么办?任何帮助,请
已编辑
这是最终的代码,谢谢
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)
答案 0 :(得分:1)
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(",")