tkinter listbox get(ACTIVE) method

前端 未结 5 1098
耶瑟儿~
耶瑟儿~ 2020-12-15 05:19

I was trying to make the currently selected Listbox item to be printed out. For example, when I select item "one", it should print out "one"

5条回答
  •  星月不相逢
    2020-12-15 05:40

    You could use this, it doesn't require the list box. So if you have multiple list boxes it will retrieve the value from any

    from tkinter import*
    root=Tk()
    sizex = 600
    sizey = 400
    posx  = 40
    posy  = 20
    root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))
    itemsforlistbox=['one','two','three','four','five','six','seven']
    
    def CurSelet(event):
        widget = event.widget
        selection=widget.curselection()
        picked = widget.get(selection[1])
        print(picked)
    
    mylistbox=Listbox(root,width=60,height=10,font=('times',13))
    mylistbox.bind('<>',CurSelet)
    mylistbox.place(x=32,y=90)
    
    for items in itemsforlistbox:
        mylistbox.insert(END,items)
    root.mainloop()
    

提交回复
热议问题