Home > Software design >  The code for submitting an answer from a Scrollbar in tkinter doesn't work
The code for submitting an answer from a Scrollbar in tkinter doesn't work

Time:01-30

So,I tried printing the selection of a scrollbar onto the console,and it does not generate what i wanted it to. When I select for example the 3rd index in the list box it doesnt return 3,but "(2,)".I neither know what this "(2,)" means,nor what I did wrong,can someone help me there please? The source code is following:

def get_entry():
    print(scrollbar.curselection())

efs = tkinter.Button(root, text="Submit list element",command=get_entry)




scrollbar = Listbox(root)
list = [1,2,3,4,5]
for element in list:
    scrollbar.insert(END, element)
scrollbar.pack(side="left")
scrollbar.config()

CodePudding user response:

There are 2 ways

Way #1: The selected option will be viewed to the user when he clicks the button 'Show Selected'

from tkinter import *

ws = Tk()
ws.title('Python Guides')
ws.geometry('400x300')
ws.config(bg='#446644')

def showSelected():
    show.config(text=lb.get(ANCHOR))


lb = Listbox(ws)
lb.pack()
lb.insert(0, 'red')
lb.insert(1, 'green')
lb.insert(2, 'yellow')
lb.insert(3, 'blue')

Button(ws, text='Show Selected', command=showSelected).pack(pady=20)
show = Label(ws)
show.pack()

ws.mainloop()

Way #2:

mylistbox.get(mylistbox.curselection())
  •  Tags:  
  • Related