如何更新条目并将其显示在列表框中

时间:2016-11-03 19:51:30

标签: python list user-interface tkinter

所以我坚持使用下面的代码:

from tkinter import *
from tkinter import ttk
import platform
import shelve
from Information import *
from StudentSubjects import *
from Degree import *
Sinfo = []
SSubject = []
SDegree = []
d = shelve.open('Student_Data')

def quit():
    global root
    root.destroy()

def add_student():
    Sname = Student_name.get()
    Ssurnname = Student_surname.get()
    Sdetail = Student_detail.get()
    Snumber = Student_number.get()
    i = Students(Sname,Ssurnname,Sdetail,Snumber)
    Sinfo.append(i)

    iName = Student_subject.get()
    iCode = Student_code.get()
    iMark1 = Student_Mark1.get()
    iMark2 = Student_Mark2.get()
    iMark3 = Student_Mark3.get()
    iProject = Student_project.get()
    j = Subjects(iName,iCode,iMark1,iMark2,iMark3,iProject)
    SSubject.append(j)

    kCourse = Degree_course.get()
    kCode = Degree_code.get()
    kYear = Degree_year.get()
    v = Degrees(kCourse,kCode,kYear)
    SDegree.append(v)

def view_student():
    Student_lb.delete(0, END)
    for s in Sinfo:
        Student_lb.insert(END,str(s))   
    Subject_lb.delete(0, END)
    for j in SSubject:
        Subject_lb.insert(END,str(j))   
    Degree_lb.delete(0, END)
    for v in SDegree:
        Degree_lb.insert(END,str(v))    

def update_student():
    Sname = Student_name.get()
    Ssurnname = Student_surname.get()
    Sdetail = Student_detail.get()
    Snumber = Student_number.get()
    i = Students(Sname,Ssurnname,Sdetail,Snumber)
    Sinfo.append(i)

    iName = Student_subject.get()
    iCode = Student_code.get()
    iMark1 = Student_Mark1.get()
    iMark2 = Student_Mark2.get()
    iMark3 = Student_Mark3.get()
    iProject = Student_project.get()
    j = Subjects(iName,iCode,iMark1,iMark2,iMark3,iProject)
    SSubject.append(j)

    kCourse = Degree_course.get()
    kCode = Degree_code.get()
    kYear = Degree_year.get()
    v = Degrees(kCourse,kCode,kYear)
    SDegree.append(v)

def delete_student():
    pass

root = Tk()
root.title("Development Software Project")
root.geometry('1200x700')

notebook = ttk.Notebook(root)
frame1 = ttk.Frame(notebook)
frame2 = ttk.Frame(notebook)
frame3 = ttk.Frame(notebook)
frame4 = ttk.Frame(notebook)
frame5 = ttk.Frame(notebook)

notebook.add(frame1, text='Student information')
notebook.add(frame2, text='View class list')
notebook.add(frame3, text='View Graphs')
notebook.add(frame4, text='Save')
notebook.add(frame5, text='Quit')
notebook.grid(column = 1, row = 1, sticky = W)


#frame 1
ttkLabelName = ttk.Label(frame1,text='Please enter Student Name: ')
ttkLabelName.grid(column=1, row=1 , sticky = W)

ttkLabelSurname = ttk.Label(frame1,text='Please enter Student Surname: ')
ttkLabelSurname.grid(column=1, row=2, sticky = W)

ttkLabelDetail = ttk.Label(frame1,text='Please enter Student Contact Number: ')
ttkLabelDetail.grid(column=3, row=1, sticky = W)

ttkLabelNumber = ttk.Label(frame1,text='Please enter Student Number: ')
ttkLabelNumber.grid(column=3, row=2, sticky = W)

Student_name = StringVar()
ttkName_entry = ttk.Entry(frame1, textvariable = Student_name)
ttkName_entry.grid(column=2,row=1, sticky = W)

Student_surname = StringVar()
ttkSurname_entry = ttk.Entry(frame1, textvariable = Student_surname)
ttkSurname_entry.grid(column=2,row=2, sticky = W)

Student_detail = StringVar()
ttkSurname_entry = ttk.Entry(frame1, textvariable = Student_detail)
ttkSurname_entry.grid(column=4,row=1, sticky = W)

Student_number = StringVar()
ttkSurname_entry = ttk.Entry(frame1, textvariable = Student_number)
ttkSurname_entry.grid(column=4,row=2, sticky = W)

Student_subject = StringVar()
ttkLabelSubject_name = ttk.Label(frame1,text='Please enter the Subject Name: ')
ttkLabelSubject_name.grid(column=1, row=3 , sticky = W)
ttkSubject_entry = ttk.Entry(frame1, textvariable = Student_subject)
ttkSubject_entry.grid(column=1,row=4, sticky = W)

Student_code = StringVar()
ttkLabelCode = ttk.Label(frame1,text='Please enter the Subject Code: ')
ttkLabelCode.grid(column=2, row=5 , sticky = W)
ttkCode_entry = ttk.Entry(frame1, textvariable = Student_code)
ttkCode_entry.grid(column=2,row=6, sticky = W)

Student_Mark1 = StringVar()
ttkLabelMark1 = ttk.Label(frame1,text='Please enter Mark 1: ')
ttkLabelMark1.grid(column=1, row=7 , sticky = W)
ttkMark1_entry = ttk.Entry(frame1, textvariable = Student_Mark1)
ttkMark1_entry.grid(column=1,row=8, sticky = W)

Student_Mark2 = StringVar()
ttkLabelMark2 = ttk.Label(frame1,text='Please enter Mark 2: ')
ttkLabelMark2.grid(column=2, row=9 , sticky = W)
ttkMark2_entry = ttk.Entry(frame1, textvariable = Student_Mark2)
ttkMark2_entry.grid(column=2,row=10, sticky = W)

Student_Mark3 = StringVar()
ttkLabelMark3 = ttk.Label(frame1,text='Please enter  Mark 3: ')
ttkLabelMark3.grid(column=1, row=11 , sticky = W)
ttkMark3_entry = ttk.Entry(frame1, textvariable = Student_Mark3)
ttkMark3_entry.grid(column=1,row=12, sticky = W)

Student_project = StringVar()
ttkLabelProject = ttk.Label(frame1,text='Please enter project Mark: ')
ttkLabelProject.grid(column=2, row=13 , sticky = W)
ttkProject_entry = ttk.Entry(frame1, textvariable = Student_project)
ttkProject_entry.grid(column=2,row=14, sticky = W)

Degree_course = StringVar()
ttkLabelProject = ttk.Label(frame1,text='Please enter Course Name: ')
ttkLabelProject.grid(column=1, row=15 , sticky = W)
ttkProject_entry = ttk.Entry(frame1, textvariable = Degree_course)
ttkProject_entry.grid(column=1,row=16, sticky = W)

Degree_code = StringVar()
ttkLabelProject = ttk.Label(frame1,text='Please enter the degree code: ')
ttkLabelProject.grid(column=2, row=17 , sticky = W)
ttkProject_entry = ttk.Entry(frame1, textvariable = Degree_code)
ttkProject_entry.grid(column=2,row=18, sticky = W)

Degree_year = StringVar()
ttkLabelProject = ttk.Label(frame1,text='Please enter the year of the Student: ')
ttkLabelProject.grid(column=1, row=19 , sticky = W)
ttkProject_entry = ttk.Entry(frame1, textvariable = Degree_year)
ttkProject_entry.grid(column=1,row=20, sticky = W)

Student_lb = Listbox(frame1,listvariable=Sinfo)
Student_lb.grid(column=1, row=21, sticky=(W, E))
Student_lb.config(width=50, height=10)

Subject_lb = Listbox(frame1,listvariable=SSubject)
Subject_lb.grid(column=2, row=21, sticky=(W, E))
Subject_lb.config(width=50, height=10)

Degree_lb = Listbox(frame1,listvariable=SDegree)
Degree_lb.grid(column=3, row=21, sticky=(W, E))
Degree_lb.config(width=50, height=10)

button_add = Button(frame1, text = "Add", command = add_student)
button_add.grid(column =0, row= 22, sticky = W)

button_view = Button(frame1, text = "View", command = view_student)
button_view.grid(column =1, row= 22, sticky = W)

button_view = Button(frame1, text = "Update", command = view_student)
button_view.grid(column =2, row= 22, sticky = W)

button_view = Button(frame1, text = "Delete", command = view_student)
button_view.grid(column =3, row= 22, sticky = W)
#EOC
#frame 2

#frame 3

#frame 4

#frame 5
strVersion = "Running Python version " + platform.python_version()
ttkLabelVersion = ttk.Label(frame5, text=strVersion)
ttkLabelVersion.grid(column=1,row=1,sticky = N)
strPlatform = "Platform: " + platform.platform()
ttkLabelPlatform = ttk.Label(frame5, text=strPlatform)
ttkLabelPlatform.grid(column=1,row=2,sticky = N)
ttkLabelBye = ttk.Label(frame5, text='Good Bye')
ttkLabelBye.grid(column=1,row=3,sticky = N)
ttkButtonQuit = ttk.Button(frame5,text="Quit",command=quit)
ttkButtonQuit.grid(column=1,row=4,sticky = E)
#EOC
root.mainloop()

因此,当我想更新列表框中的内容时,我所困扰的地方是我的“def update_student”,它只是输入一个重复的记录但不更新现有的记录。所以例如我想要的是See example code 因此,如果用户将学生姓名更改为John和他的Mark 2至90,我希望它在现有列表框中更改它而不添加另一个。 我该怎么做呢?

0 个答案:

没有答案