Skip to content

Instantly share code, notes, and snippets.

@vigilantPotato
Created May 21, 2024 21:23
Show Gist options
  • Save vigilantPotato/5effb11e3315ff391f38b0cac50b0096 to your computer and use it in GitHub Desktop.
Save vigilantPotato/5effb11e3315ff391f38b0cac50b0096 to your computer and use it in GitHub Desktop.
How to use listbox (set item and get selected)
import ctypes
import tkinter
def get_selected(event):
n = listbox.curselection() #get index no.
l["text"] = listbox.get(n) #set label text
if __name__ == "__main__":
ctypes.windll.shcore.SetProcessDpiAwareness(1)
root = tkinter.Tk()
#label
l = tkinter.Label(
root,
text = "select item",
width = 15,
bg = "lightblue",
)
l.pack()
#listobox
item_list = ["Easy", "Normal", "Hard"]
listbox = tkinter.Listbox(root)
for item in item_list:
listbox.insert(tkinter.END, item)
listbox.bind("<<ListboxSelect>>", get_selected)
listbox.pack()
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment