Python Forum
How to get the selected item from Listbox and convert it to int?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get the selected item from Listbox and convert it to int?
#2
(Sorry, I've missed the code, here is the proper post:)

Hi,

how can I convert the selected item from Listbox and convert into integer for using in my code?

Here is the code:---------------------------------------------------------------------------------------------
from tkinter import*
root=Tk()

sizex = 600
sizey = 400
posx  = 40
posy  = 20

root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))

def Select(a):
    a = int(mylistbox.get(ANCHOR))
    if a == 1:
        a = 2 
        return(a)
    elif a == 2:
        a = 3
        return(a) 
   
            
mylistbox=Listbox(root,height=2, width=25, font=('times',13))
mylistbox.bind('<<ListboxSelect>>',Select)
#mylistbox.insert(1,2)
itemsforlistbox=['1','2']
mylistbox.place(x=32,y=90)

scroll = Scrollbar(root, orient=VERTICAL) # the allignment of the scrollbar
mylistbox["yscrollcommand"]=scroll.set # link the list with the scroll
scroll["command"]=mylistbox.yview # link the scroll with the scroll

mylistbox.grid(row=5, column=5)
scroll.grid(row=5, column=5, sticky=N+S+E)

for items in itemsforlistbox:
    mylistbox.insert(END,items)
    
root.mainloop()
-----------------------------------------------------------------------------------------------------------------------------------
I already converted the item into an integer and I can return it with a new value, but how can I use this value in my code? I've tried anything I can imagine, for example I used the variable b, but I always get an error:

---------------------------------------------------------------------------------------------------------------------------------------
from tkinter import*
root=Tk()

sizex = 600
sizey = 400
posx  = 40
posy  = 20

root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))

def Select(a):
    a = int(mylistbox.get(ANCHOR))
    if a == 1:
        a = 2 
        return(a)
    elif a == 2:
        a = 3
        return(a) 
    
b = Select(a) + 2 
            
mylistbox=Listbox(root,height=2, width=25, font=('times',13))
mylistbox.bind('<<ListboxSelect>>',Select)
#mylistbox.insert(1,2)
itemsforlistbox=['1','2']
mylistbox.place(x=32,y=90)

scroll = Scrollbar(root, orient=VERTICAL) # the allignment of the scrollbar
mylistbox["yscrollcommand"]=scroll.set # link the list with the scroll
scroll["command"]=mylistbox.yview # link the scroll with the scroll

mylistbox.grid(row=5, column=5)
scroll.grid(row=5, column=5, sticky=N+S+E)

for items in itemsforlistbox:
    mylistbox.insert(END,items)
    
root.mainloop()
Reply


Messages In This Thread
How to get the selected item from Listbox and convert it to int? - by Jionni - Feb-15-2020, 10:51 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  QListWidget, no item was selected flash77 4 1,074 Aug-02-2023, 09:31 AM
Last Post: Axel_Erfurt
  [PyQt] How to always select the item in the first column when a row is selected DrakeSoft 1 1,699 Feb-17-2023, 07:43 PM
Last Post: DrakeSoft
  [Tkinter] the first item in listbox rwahdan 4 3,298 Jun-27-2021, 03:48 PM
Last Post: rwahdan
  Update value selected in option menu when select an item from text box klllmmm 2 5,094 Jun-06-2019, 04:51 AM
Last Post: klllmmm
  [Tkinter] taking bad listbox items after lowering it to 1 item deadmarshal 3 3,163 Jul-18-2018, 04:16 PM
Last Post: woooee
  seprating columns of an listbox item and putting them into different entry gray 2 4,295 Feb-26-2017, 12:53 PM
Last Post: gray

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020