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?
#1
Hi,

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

Here is the code:

İmage


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:

İmage
Reply
#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
#3
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()

Does anybody have a clue, how I can solve this??
Reply
#4
Hi Jionni

Please try out the following snippet:
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 Show(event):
    global b
    print(b)
    
def Select(event):
    global b
    
    a = int(mylistbox.get(ANCHOR))

    if a == 1:
        a = 2 
    elif a == 2:
        a = 3
        
    b = a + 2

result = 0     
itemsforlistbox=['1','2'] 
             
mylistbox = Listbox(root, height=2, width=25, font=('times',13))
mylistbox.bind('<<ListboxSelect>>', Select)
mylistbox.insert(END, *itemsforlistbox)
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)
 
root.bind('<Return>', Show)
     
root.mainloop()
wuf :-)
Reply
#5
Hi wuf!!

Ok, first of all, thanks for answering! Now, using your code, the global variable b doesn´t show up in my console. Only if I say: print(b) und put it below: b = a + 2, then b displays.
But actually, I want to return the value of b and use it, for example: c = b + 2. But it doesn't work. It is always the same error: NameError: name 'b' is not defined.

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 Show(event):
    global b
    print(b)
    
    
     
def Select(event):
    global b
     
    a = int(mylistbox.get(ANCHOR))
 
    if a == 1:
        a = 2 
    elif a == 2:
        a = 3
         
    b = a + 2
    print(b)
    
c = b + 2 # doesn't work    
    
result = 0
itemsforlistbox=['1','2'] 
              
mylistbox = Listbox(root, height=2, width=25, font=('times',13))
mylistbox.bind('<<ListboxSelect>>', Select)
mylistbox.insert(END, *itemsforlistbox)
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)
  
root.bind('<Return>', Show)
      
root.mainloop()
Reply
#6
OK Jionni

May be this script does show more about the situation:
from tkinter import*
root=Tk()
 
sizex = 600
sizey = 400
posx  = 100
posy  = 100
 
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))

b = 0

# This function 'select' is reserved for the listbox select event only! 
# That means this function is called as soon you select an item in the listbox
# The parameter 'a' is wrong. It is called 'event'
#def Select(a):
def select(event):
    global b
    a = int(mylistbox.get(ANCHOR))
    
    b = calculation(a) + 2
    # Now the global variable 'b' is set to the result of your calculation
    
def calculation(a):
    if a == 1:
        a = 2 
        return(a)
    elif a == 2:
        a = 3
        return(a) 

def show_var_b(event):
    global b
    print("The value of variable 'b' is {}".format(b))
     
# As above explained 'select' is reserved for the listbox select event an not
# for a call from the global environment    
#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)

# After you have selected an item in the listbox the global variable 'b' is
# available for further usage. By pressing 'Return' (Enter) on your keyboard
# the result of the global variable 'b' is printed out. 
root.bind('<Return>', show_var_b)
     
root.mainloop()
wuf :-)
Reply
#7
Hey wuf,

this help me a lot, you are amazing... But there is something I don't get yet.

How can I use the variable b for further coding? Ok, I can print out the result by pressig the ENTER-button, thats really nice and useful. But how can I assign the variable b, for example, to variable z, or taking the value of b and use it for another function.
You know what I mean? I want to use the value of b in another part of the code...
Reply
#8
Ok Jionni

Lets make a simple application. By activating the button the app will multiply your listbox selection witch is stored in the variable 'b' with the constant PI:
import tkinter as tk

# Constants for the geometry of the main window
APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 300
APP_HEIGHT = 300
APP_TITLE = "Simple Applikation"
PI = 3.1415

# Variables
b = 0
items_for_listbox = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Functions
def select(event):
    global b
    b = int(mylistbox.get(tk.ANCHOR))
    selected_var.set(b)

def calculate():
    global b
    result = b * PI
    result_var.set(result)
        
# GUI
# App window
app_win = tk.Tk()
app_win.title(APP_TITLE) 
app_win.wm_geometry("%dx%d+%d+%d" % (APP_WIDTH, APP_HEIGHT, APP_XPOS, APP_YPOS))

# Main container embeded in the app window
# Container for all of the app widgets
main_frame = tk.Frame(app_win)
main_frame.pack(expand=tk.YES, fill=tk.BOTH)

# Label to show your listbox selection
selected_var = tk.StringVar()
tk.Label(main_frame, textvariable=selected_var, font=('times', 30, 'bold')
    ).pack(expand=tk.YES)
selected_var.set("No selection")

# Label to show your calculated result
result_var = tk.StringVar()
tk.Label(main_frame, textvariable=result_var, font=('times', 30, 'bold')
    ).pack(expand=tk.YES)
result_var.set("No result")

# Container for the listbox & scrollbar (embeded in the main frame)
listbox_frame = tk.Frame(main_frame)
listbox_frame.pack(expand=tk.YES)
    
mylistbox = tk.Listbox(listbox_frame, height=5, width=25, font=('times',13))
mylistbox.bind('<<ListboxSelect>>', select)
mylistbox.grid(row=0, column=0)
mylistbox.insert(tk.END, *items_for_listbox)
 
scroll = tk.Scrollbar(listbox_frame, orient=tk.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
scroll.grid(row=0, column=1, sticky=tk.N+tk.S) #sticky=N+S+E)

# Button to launch a calculation
tk.Button(main_frame, text="Calculate (Listbox selection * PI)",
    command=calculate).pack(expand=tk.YES)
   
app_win.mainloop()
wuf :-)
Reply
#9
Wow... now I got it, I think!!hahaha

The thing is I've written a code for calculating the reverberation time in a room. So, the code was not the problem. I've never thought, that the gui takes so many time...
In the next weeks, I will ask further questions about the gui and I hope you will help me again. :)

Thanks!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  QListWidget, no item was selected flash77 4 967 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,631 Feb-17-2023, 07:43 PM
Last Post: DrakeSoft
  [Tkinter] the first item in listbox rwahdan 4 3,199 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,009 Jun-06-2019, 04:51 AM
Last Post: klllmmm
  [Tkinter] taking bad listbox items after lowering it to 1 item deadmarshal 3 3,128 Jul-18-2018, 04:16 PM
Last Post: woooee
  seprating columns of an listbox item and putting them into different entry gray 2 4,239 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