使用tkinter将txt文件加载到列表框

时间:2018-12-07 18:28:24

标签: python tkinter

我只是不能使此加载按钮起作用,因此它会将output.txt文件加载到我的列表框中。我是160岁的第一年级学生,没有编程背景,我试图将这本愚蠢的电话簿作为我的第一个“大项目”,以使我对自己正在做的事情有所了解,而ive使我的其他工作除加载功能外output.txt文件。原谅我糟糕的编码,确保xd可能好一百万倍

from tkinter import *

win = Tk()

def delete():
     select=listbox.curselection()
     index=select[0]
     listbox.delete(index)

def returnEntry(arg=None):

    fname = e1.get()
    lname = e2.get()
    number = e3.get()
    listbox.insert(END, fname+ ' ' + lname+ ' ' + number)

def save():
    list1=list(listbox.get(0,END))
    f=open("output.txt", "w")
    f.writelines(str(list1))
    f.close()

def load():
     with open("output.txt", "r") as f:
          output=f.read()



Label(win, text="First Name").grid(row=0)
Label(win, text="Last Name").grid(row=1)
Label(win, text="Phone Number").grid(row=2)

resultLabel = Label(win, text = "")
resultLabel.grid(row=4, column=1)

resultLabel1 = Label(win, text = "")
resultLabel1.grid(row=4, column=2)

resultLabel2 = Label(win, text = "")
resultLabel2.grid(row=4, column=3)


fname=StringVar()
e1 = Entry(win, textvariable=fname)

lname=StringVar()
e2 = Entry(win, textvariable=lname)

number=StringVar()
e3 = Entry(win, textvariable=number)

scrollbar=Scrollbar(win, orient=VERTICAL)

listbox=Listbox(win, selectmode=EXTENDED, yscrollcommand=scrollbar.set,width=40) 
listbox.grid(row=4, columnspan=3)

scrollbar.config(command=listbox)


b1=Button(win, text="Add", command = returnEntry)
b2=Button(win, text="Delete",  command=delete)
b3=Button(win, text="Save", command=save)
b4=Button(win, text="load", command=load)


win.bind("<Return>", returnEntry)

e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1)
b1.grid(row=3, column=1, sticky=W)
b2.grid(row=3, column=1)
b3.grid(row=3, column=1, sticky=E)
b4.grid(row=3, column=2, sticky=W)

win.mainloop()

0 个答案:

没有答案