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

2024-10-01 22:33:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个程序,允许用户编辑下面的Listbox小部件。我有一个(getactive)delete配置,可以从列表中直观地删除一个项目。但是我没有幸从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()

Tags: textrightmodulescolumnresultframelabellist
1条回答
网友
1楼 · 发布于 2024-10-01 22:33:49

您只需使用index = listbox.curselection()listbox中获取所选项目,然后使用listbox.delete(index)从{}中删除该项目。在

要向listbox添加新项,您需要使用任何输入方法获取该项,例如tkinter.simpledialog,然后使用listbox.insert('end', item)将该项附加到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)

然后修改AddDelete按钮来调用相应的函数。在

顺便说一句,您的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],因为列表中的mod_code并不都是7个字符。在

相关问题 更多 >

    热门问题