TKINTER:如何从列表框中删除项目/将项目添加到列表框中

时间:2019-01-02 23:04:53

标签: python-3.x tkinter

我是编程新手,要进行分配,我必须将项目添加到列表框中,然后从列表框中删除项目。我一直在努力,看了一些例子。

“删除”按钮以可视方式删除列表,但在单击该项目返回的条目时将其与跟踪功能结合在一起。我想我已将列表设置为元组,并且不确定如何配置,因此该项目不见了。

添加按钮尚未配置,因为我不知道如何修改列表,如果有人对我如何实现此操作有任何帮助的信息?预先感谢。

代码:

import tkinter
from tkinter import *

main = Tk()

var = StringVar

def delete_Listbox():
    listbox.delete(ACTIVE)

def print_Listbox():
    z=listbox.get(0, END)
    print (z)

def on_entry_click(event):
    if searchbox.get() == 'Search module code or name':
       searchbox.delete(0, "end") # delete all the text in the entry
       searchbox.insert(0, '') #Insert blank for user input

def courseIndex(*args):
  course_input = search_var.get()
  listbox.delete(0, END)
  for item in all_items:
    if course_input.lower() in item.lower():
      listbox.insert(END, item)

search_var = StringVar()
search_var.trace('w', courseIndex)

searchbox = Entry(main, textvariable=search_var)
searchbox.grid(row=1,column=0)

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

searchbox = Entry(main, textvariable=search_var, bd=1, width=26)
searchbox.insert(0, 'Search module code or name')
searchbox.bind('<FocusIn>', on_entry_click)

listbox = Listbox(main, font = ("Purisa", 10, "bold"), height=20, width=55)

for i in [
    'CLD4002: Introduction to Operating Systems Virtualisation',
    'CLD4003: Linux Essentials',
    'SEC4001: Introduction to Networking',
    'SEC4002: Routing Fundamentals',
    'SEC4003: Security Fundamentals',
    'SWE4001: Introduction to Software Development',
    'CLD5003: Server Application Configuration and Management',
    'CLD5005: Advanced Linux',
    'SEC5001: Computing Security',
    'SEC5002: Network Architecture',
    'SEC5003: Wide Area Networks',
    'SEC5004: Wireless and Mobile Networks',
    'SEC5005: Enterprise Infrastructure',
    'CLD6000: Contemporary Problems Analysis',
    'CDL6001: Undergraduate Research Project',
    'SEC6001: Network Management',
    'SEC6002: Ethical Hacking and Digital Forensics',
    'SEC6003: Operations Management',
    'SEC6004: Cloud and Network Security',
    'SWE6008: Advanced Database'
]:
  listbox.insert(END, i)
listbox.grid()
all_items = listbox.get(0, END)

b1 = Button(main, text = "Add", font = ("Purisa", 10, "bold"), command = delete_Listbox)
b1.grid(row = 4, column = 0)

b3 = Button(main, text="Print", font = ("Purisa", 10, "bold"), command=print_Listbox)
b3.grid(row=3, column=1, columnspan=1)

b3 = Button(main, text="Delete", font = ("Purisa", 10, "bold"), command=delete_Listbox)
b3.grid(row=5, column=0)


searchbox.grid(row=1,column=0)




main.mainloop()

1 个答案:

答案 0 :(得分:0)

查看我注释掉的行。您正在通过listboxtrace循环用all_items = listbox.get(0, END)函数重新插入所有for item in all_items:

import tkinter
from tkinter import *

main = Tk()

var = StringVar

def delete_Listbox():
    listbox.delete(ACTIVE)

def print_Listbox():
    z=listbox.get(0, END)
    print (z)

def on_entry_click(event):
    if searchbox.get() == 'Search module code or name':
       searchbox.delete(0, "end") # delete all the text in the entry
       searchbox.insert(0, '') #Insert blank for user input

def courseIndex(*args):
  course_input = search_var.get()
  listbox.delete(0, END)
  for item in all_items:
    if course_input.lower() in item.lower():
      listbox.insert(END, item)

search_var = StringVar()
#search_var.trace('w', courseIndex)

searchbox = Entry(main, textvariable=search_var)
searchbox.grid(row=1,column=0)

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

searchbox = Entry(main, textvariable=search_var, bd=1, width=26)
searchbox.insert(0, 'Search module code or name')
searchbox.bind('<FocusIn>', on_entry_click)

listbox = Listbox(main, font = ("Purisa", 10, "bold"), height=20, width=55)

for i in [
    'CLD4002: Introduction to Operating Systems Virtualisation',
    'CLD4003: Linux Essentials',
    'SEC4001: Introduction to Networking',
    'SEC4002: Routing Fundamentals',
    'SEC4003: Security Fundamentals',
    'SWE4001: Introduction to Software Development',
    'CLD5003: Server Application Configuration and Management',
    'CLD5005: Advanced Linux',
    'SEC5001: Computing Security',
    'SEC5002: Network Architecture',
    'SEC5003: Wide Area Networks',
    'SEC5004: Wireless and Mobile Networks',
    'SEC5005: Enterprise Infrastructure',
    'CLD6000: Contemporary Problems Analysis',
    'CDL6001: Undergraduate Research Project',
    'SEC6001: Network Management',
    'SEC6002: Ethical Hacking and Digital Forensics',
    'SEC6003: Operations Management',
    'SEC6004: Cloud and Network Security',
    'SWE6008: Advanced Database'
]:
  listbox.insert(END, i)
listbox.grid()
#all_items = listbox.get(0, END)

b1 = Button(main, text = "Add", font = ("Purisa", 10, "bold"), command = delete_Listbox)
b1.grid(row = 4, column = 0)

b3 = Button(main, text="Print", font = ("Purisa", 10, "bold"), command=print_Listbox)
b3.grid(row=3, column=1, columnspan=1)

b3 = Button(main, text="Delete", font = ("Purisa", 10, "bold"), command=delete_Listbox)
b3.grid(row=5, column=0)


searchbox.grid(row=1,column=0)




main.mainloop()