Skip to content

Instantly share code, notes, and snippets.

@vigilantPotato
Created May 21, 2024 21:24
Show Gist options
  • Save vigilantPotato/67d85a6146e96f72f1696f304fa6f833 to your computer and use it in GitHub Desktop.
Save vigilantPotato/67d85a6146e96f72f1696f304fa6f833 to your computer and use it in GitHub Desktop.
How to up add item to listbox (with listvariable)
import ctypes
import tkinter
def get_selected(event):
n = listbox.curselection() #get index no.
l["text"] = listbox.get(n) #set label text
def input(event):
item_list.append(entry.get()) #add item
list_var.set(item_list) #set variable
entry.delete(0, tkinter.END) #clear entry
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"]
list_var = tkinter.StringVar(root, value=item_list)
listbox = tkinter.Listbox(root, listvariable=list_var)
listbox.bind("<<ListboxSelect>>", get_selected)
listbox.pack()
#entry
entry = tkinter.Entry(root, bd=5)
entry.bind("<Return>", input) #bind Retun key
entry.pack(pady=3)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment