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?
#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


Messages In This Thread
RE: How to get the selected item from Listbox and convert it to int? - by wuf - Feb-16-2020, 11:23 PM

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