forked from flatplanet/Intro-To-TKinter-Youtube-Course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listbox2.py
83 lines (53 loc) · 1.91 KB
/
listbox2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from tkinter import *
root = Tk()
root.title('Codemy.com')
root.iconbitmap('c:/gui/codemy.ico')
root.geometry("400x600")
my_frame = Frame(root)
my_scrollbar = Scrollbar(my_frame, orient=VERTICAL)
# Listbox!
my_listbox = Listbox(my_frame, width=50, selectmode=MULTIPLE, yscrollcommand=my_scrollbar.set)
my_scrollbar.config(command=my_listbox.yview)
my_scrollbar.pack(side=RIGHT, fill=Y)
my_listbox.pack(pady=15)
my_frame.pack()
#Add item to listbox
my_listbox.insert(END, "This is an item")
my_listbox.insert(END, "Second Item!")
# Add list of items
global my_list
my_list = ["One", "Two", "Three", "One", "Two", "Three", "One", "Two", "Three", "One", "Two", "Three", "One", "Two", "Three", "One", "Two", "Three", "One", "Two", "Three", "One", "Two", "Three", "One", "Two", "Three", "One", "Two", "Three", ]
for item in my_list:
my_listbox.insert(END, item)
def delete():
my_listbox.delete(ANCHOR)
my_label.config(text='')
def select():
my_label.config(text=my_listbox.get(ANCHOR))
def delete_all():
my_listbox.delete(0, END)
def select_many():
#my_label.config(text=my_listbox.curselection())
#print(my_listbox.curselection())
result = ''
for thing in my_listbox.curselection():
result = result + str(my_listbox.get(thing)) + '\n'
my_label.config(text=result)
def delete_many():
for thing in reversed(my_listbox.curselection()):
my_listbox.delete(thing)
my_label.config(text='')
my_button = Button(root, text="Delete", command=delete)
my_button.pack(pady=10)
my_button2 = Button(root, text="Select", command=select)
my_button2.pack(pady=10)
global my_label
my_label = Label(root, text='')
my_label.pack(pady=5)
my_button3 = Button(root, text="Delete All", command=delete_all)
my_button3.pack(pady=10)
my_button4 = Button(root, text="select many", command=select_many)
my_button4.pack(pady=10)
my_button5 = Button(root, text="Delete many", command=delete_many)
my_button5.pack(pady=10)
root.mainloop()