-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlistbox.py
More file actions
80 lines (65 loc) · 2.07 KB
/
listbox.py
File metadata and controls
80 lines (65 loc) · 2.07 KB
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
from tkinter import *
# Creating functions
def order(): # Function to order an Item
curSel = listbox.curselection()
if curSel:
print("Successfully ordered!")
i = 1
for ind in curSel:
print(f" {i}). {listbox.get(ind)}")
i += 1
def addItem(): # Function to add an Item
item = newItem.get()
foodList.append(item)
listbox.insert(len(foodList)-1, item)
print(f"Successfully Added {item}!")
setHeight()
def delItem(): # Function to delete an Item
curSel = listbox.curselection()
print("Successfully Removed!")
i = 1
for ind in reversed(curSel):
item = listbox.get(ind)
foodList.remove(item)
listbox.delete(ind)
print(f" {i}). {item}")
i += 1
setHeight()
def setHeight(): # Function to set the height of the ListBox
listbox.config(height=listbox.size())
# Creating Window
window = Tk()
# Styling Window
window.title("Listbox Program Tkinter")
window.config(bg="black")
# Creating the listbox
foodList = ["Pizza", "Fries", "Apple", "Soda", "Biryani", "Paneer", "Ice Cream", "Salad", "Chai"]
listbox = Listbox(window,
bg="#f7ffde",
font=("Constantia", 35),
selectmode=MULTIPLE,
width=12)
listbox.pack()
for food in foodList:
ind = foodList.index(food)
listbox.insert(ind, food)
setHeight()
# Creating an Input Entry for Any New Item
newItem = Entry(window,
fg="black",
bg="#f7ffde",
font=("Consonatia", 35),
width=10)
newItem.pack()
newItem.insert(0, "New Item")
# Creating a Submit Button
subBtn = Button(window, text="Submit", command=order, padx=10)
subBtn.pack()
# Creating Submit Button for Input Field
addBtn = Button(window, text="Add Item", command=addItem, padx=10)
addBtn.pack()
# Creating the Delete Button
delBtn = Button(window, text="Remove Item", command=delItem, padx=10)
delBtn.pack()
# Displaying Window
window.mainloop()