External Exam Download Resources Web Applications Games Recycle Bin

Listbox

listbox.py

from tkinter import *
from tkinter import messagebox
window = Tk()

#CONTROLS + VARIABLES:
lstDrinks = Listbox()
for numberInList, drinkName in enumerate(["Coke", "Fanta", "Sprite"]):
    lstDrinks.insert(numberInList, drinkName)
lstDrinks.select_set(0) #sets default selection as first drink   
btnOrder = Button(text="Order")

#EVENTS:
def order(event):
    drink = lstDrinks.get(lstDrinks.curselection())
    messagebox.showinfo("drink selection","you ordered: " + drink)

#BINDINGS:
btnOrder.bind("<ButtonPress>", order)

#PACK:
lstDrinks.pack(side="top", anchor="nw")
btnOrder.pack(side="top", anchor="nw")

#RUN:
window.mainloop()