here2share

# Tk_Listbox_Demo.py

Oct 24th, 2020
494
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Tk_Listbox_Demo.py
  2.  
  3. from tkinter import *
  4.  
  5. root = Tk()
  6. root.title("Listbox Demo")
  7.  
  8. def on_add():
  9.     text = txtContent.get()
  10.     listbox.insert(END, text)
  11.  
  12. def on_del():
  13.     selections = listbox.curselection()
  14.     for i in reversed(selections):
  15.         listbox.delete(i)
  16.  
  17. def on_show():
  18.     selections = listbox.curselection()
  19.     result = ""
  20.     for i in selections:
  21.         result += str(listbox.get(i))
  22.     txtContent.delete(0, END)
  23.     txtContent.insert(0, result)
  24.  
  25. btnAdd = Button(root, text = 'Add', command = on_add)
  26. txtContent = Entry(root, width = 20)
  27. btnDel = Button(root, text = 'Del', command = on_del)
  28. btnShow = Button(root, text = 'Show', command = on_show)
  29. listbox = Listbox(root, selectmode = EXTENDED)
  30. listbox.grid(columnspan = 4)
  31. listbox['width'] = 40
  32.  
  33. grids = [
  34.     (btnAdd, 0, 0),
  35.     (txtContent, 0, 1),
  36.     (btnDel, 0, 2),
  37.     (btnShow, 0, 3),
  38.     (listbox, 1, 0)
  39. ]
  40.  
  41. for obj, r, c in grids:
  42.     obj.grid(row = r, column = c)
  43.  
  44. for i in range(1, 10):
  45.     listbox.insert(END, i)
  46.  
  47. root.mainloop()
RAW Paste Data