Created
May 21, 2024 21:23
-
-
Save vigilantPotato/5effb11e3315ff391f38b0cac50b0096 to your computer and use it in GitHub Desktop.
How to use listbox (set item and get selected)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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