简体   繁体   中英

Add/delete items from tkinter Listbox permanently

I am trying to create a program that will allow the user to edit the Listbox widget below. I had a (getactive) delete configuration that would visually delete an item from the list. But I had no luck permanently adding or deleting items from the Listbox widget.

Can anybody help me understand how I would configure the Listbox widget to do the above features?

from tkinter import *

modules_list = [
    'CLD4002: Introduction to Operating Systems Virtualisation',
    'CLD4003: Linux Essentials',
    'SEC4001: Introduction to Networking',
    'SEC4002: Routing Fundamentals',
    'SEC4003: Security Fundamentals',
    'SWE4001: Introduction to Software Development',
    'CLD5005: Advanced Linux',
    'SEC5001: Computing Security',
    'SEC5002: Network Architecture',
    'SEC5003: Wide Area Networks',
    'SEC5005: Enterprise Infrastructure',
    'HE5: CHOSEN OPTIONAL MODULE',
    'CLD6000: Contemporary Problems Analysis',
    'CDL6001: Undergraduate Research Project',
    'SEC6003: Operations Management',
    'SEC6004: Cloud and Network Security',
    'HE6: CHOSEN OPTIONAL MODULE'
]

entries=[]
AVERAGE_TOT = 0 # global variable
CLASSIFICATION = "not Classified" # global variable

def print_Listbox():
    z = listbox.get(0, END)
    print (z)
    # YEAR ONE LABELS



    y1 = Label (right_frame, text="Enter Grade")
    y1.grid(row=1, column=4)

    row_offset = 0+2
    for module in modules_list:
        #Create labesl from modules_list
        lbl = Label(right_frame, text=module)
        lbl.grid(row=row_offset, column=3)
        mod_code = module[:7] # splitting the string at the 7th character from the beginint
        # create entry fields based on number of modules in modules_list
        ent= Entry(right_frame, textvariable=mod_code)
        ent.grid(row=row_offset, column=4)
        entries.append(ent)
        row_offset+=1


    classification = Label (right_frame, text="Your degree classification is :" + CLASSIFICATION)
    average_result = Label (right_frame, text="Your average is " + str(AVERAGE_TOT))
    # FINAL AWARD CONFIGURATIONS

    classification.grid(row=len(modules_list)+2, column=4)
    average_result.grid(row=len(modules_list)+3, column=4)



    b1 = Button (right_frame, text="press", command=lambda: setAverage(classification,average_result))
    b1.grid(row=len(modules_list)+4, column=4)


def setAverage(classification, average_result):
    total = 0
    for entry in entries:
        thisent = entry.get()
        total += int(thisent)

    average = total / len(entries)

    if average <=39:
        degreeclass = "fail"

    if average >=40 and average <=49:
        degreeclass = "3rd"

    if average >=50 and average <=59:
        degreeclass = "2:2"

    if average >=60 and average <=69:
        degreeclass = "2:2"

    if average >=70:
        degreeclass = "1st"

    average_result.config(text="Your percentage is :" + str(average))
    classification.config(text="Your degree classification is :" + degreeclass)



main = Tk()
var = StringVar

left_frame = Frame(main)
left_frame.grid(row=0, column=0)

middle_frame = Frame(main)
middle_frame.grid(row=0, column=1)

right_frame = Frame(main)
right_frame.grid(row=0, column=2)

l1 = Label(left_frame, text="Search")
l1.grid(row=0, column=0)

listbox = Listbox(left_frame, font = ("Purisa", 10, "bold"), height=20, width=55)
for i in modules_list:
  listbox.insert(END, i)
listbox.grid(rowspan=10)
all_items = listbox.get(0, END)

b1 = Button(middle_frame, text="Add", font = ("Purisa", 10, "bold"))
b1.grid(row=3, column=1, columnspan=1)

b2 = Button(middle_frame, text="Print", font = ("Purisa", 10, "bold"), command=print_Listbox)
b2.grid(row=4, column=1, columnspan=1)

b3 = Button(middle_frame, text="Delete", font = ("Purisa", 10, "bold"))
b3.grid(row=5, column=1, columnspan=1)

main.mainloop()

You can simply use index = listbox.curselection() to get the selected item in the listbox and then use listbox.delete(index) to remove the item from the listbox .

To add new item to listbox , you need to get the item using any input method, for example tkinter.simpledialog , and then use listbox.insert('end', item) to append the item to the listbox .

Therefore, define two new functions for adding and deleting item from listbox :

from tkinter import simpledialog

def add_item():
    item = simpledialog.askstring("Input", "Enter item name:")
    if item is not None:
        listbox.insert('end', item)

def delete_item():
    index = listbox.curselection()
    listbox.delete(index)

Then modify Add and Delete buttons to call the corresponding function.

BTW, there are issues in your print_Listbox function:

  • for module in modules_list: should be for module in z:
  • mod_code = module[:7] # splitting the string at the 7th character from the beginint should be mod_code = module.split(':')[0] because the mod_code in your list are not all 7 characters long.

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