从tkinter列表框中永久添加/删除项目

时间:2019-01-10 22:38:08

标签: python tkinter

我正在尝试创建一个程序,该程序将允许用户编辑下面的Listbox小部件。我有一个(getactive)删除配置,可以从列表中直观地删除一个项目。但是我没有从Listbox小部件中永久添加或删除项目的运气。

有人可以帮助我了解如何配置Listbox小部件以实现上述功能吗?

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()

1 个答案:

答案 0 :(得分:0)

您可以简单地使用index = listbox.curselection()listbox中获取选定的项目,然后使用listbox.delete(index)listbox中删除该项目。

要将新项目添加到listbox,您需要使用任何输入法(例如tkinter.simpledialog)获取该项目,然后使用listbox.insert('end', item)将该项目附加到{{1 }}。

因此,定义了两个新功能,用于从listbox添加和删除项目:

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) Add按钮以调用相应的功能。

顺便说一句,您的Delete函数中存在问题:

  • print_Listbox应该是for module in modules_list:
  • for module in z:应该为mod_code = module[:7] # splitting the string at the 7th character from the beginint,因为列表中的mod_code = module.split(':')[0]长度不全为7个字符。