forked from flatplanet/Intro-To-TKinter-Youtube-Course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listbox.py
79 lines (53 loc) · 1.85 KB
/
listbox.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
from tkinter import *
root = Tk()
root.title('Codemy.com')
root.iconbitmap('c:/gui/codemy.ico')
root.geometry("400x600")
# Create frame and scrollbar
my_frame = Frame(root)
my_scrollbar = Scrollbar(my_frame, orient=VERTICAL)
# Listbox!
# SINGLE, BROWSE, MULTIPLE, EXTENDED
my_listbox = Listbox(my_frame, width=50, yscrollcommand=my_scrollbar.set, selectmode=MULTIPLE)
#configure scrollbar
my_scrollbar.config(command=my_listbox.yview)
my_scrollbar.pack(side=RIGHT, fill=Y)
my_frame.pack()
my_listbox.pack(pady=15)
#Add item to listbox
my_listbox.insert(END, "This is an item")
my_listbox.insert(END, "Second Item!")
# Add list of items
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", ]
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_all():
result = ''
for item in my_listbox.curselection():
result = result + str(my_listbox.get(item)) + '\n'
my_label.config(text=result)
def delete_multiple():
for item in reversed(my_listbox.curselection()):
my_listbox.delete(item)
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 All", command=select_all)
my_button4.pack(pady=10)
my_button5 = Button(root, text="Delete Multiple", command=delete_multiple)
my_button5.pack(pady=10)
root.mainloop()