Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The code for submitting an answer from a Scrollbar in tkinter doesn't work

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()

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading