import tkinter as tk
my_w = tk.Tk()
my_w.geometry("400x250") # Size of the window
my_w.title("www.plus2net.com") # Window title
font1=['Arial',15,'normal'] # Font type,size and style
lb1 = tk.Listbox(my_w,height=6,font=font1,bg='lightgreen')
lb1.grid(row=1,column=1,padx=30,pady=20)
lb1.insert(1,'PHP') # Adding element to Listbox
lb1.insert(2,'MySQL')
lb1.insert(3,'Python')
lb1.insert(4,'JavaScript')
my_w.mainloop() # Keep the window open
Tkinter Listbox widget to show options for user selection
Index of Listbox elements starts from 0 .
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("500x500") # Size of the window
lb1 = tk.Listbox(my_w)
lb1.grid(row=1,column=1)
lb1.insert(1,'PHP') # Adding one element to Listbox
lb1.insert(2,'Python')
lb1.insert(3,'MySQL')
print(lb1.get(0)) # Output PHP
print(lb1.get(2)) # Output MySQL
my_w.mainloop() # Keep the window open
Add elements by using a List and displaying the first element using ACTIVE
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("500x300") # Size of the window
lb1 = tk.Listbox(my_w)
lb1.grid(row=1,column=1)
my_list=['PHP','Python','MySQL']
for element in my_list:
lb1.insert(tk.END,element)
print(lb1.get(tk.ACTIVE)) # print the first element only
my_w.mainloop() # Keep the window open
Read and print the selected element on Click event of a button.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("500x500") # Size of the window
def my_upd():
print(lb1.get(tk.ACTIVE)) # The selected element
lb1 = tk.Listbox(my_w,height=4)
lb1.grid(row=1,column=1)
my_list=['PHP','Python','MySQL']
for element in my_list:
lb1.insert(tk.END,element)
b1 = tk.Button(my_w, text='Show', width=10,
bg='yellow',command=lambda: my_upd())
b1.grid(row=1,column=2)
my_w.mainloop() # Keep the window open
Read the selected element on click.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("500x500") # Size of the window
def my_upd(my_widget):
my_w = my_widget.widget
index = int(my_w.curselection()[0])
value = my_w.get(index)
print ("You selected item ",index, value)
lb1 = tk.Listbox(my_w,height=4)
lb1.grid(row=1,column=1)
my_list=['PHP','Python','MySQL']
for element in my_list:
lb1.insert(tk.END,element)
lb1.bind('<<ListboxSelect>>', my_upd)
my_w.mainloop() # Keep the window open
In above code we can replace the code inside my_upd() like this also.
def my_upd(my_widget):
selected_indices = lb1.curselection() # index of selected
print(selected_indices) # the tuple with selected index
print(lb1.get(selected_indices)) # value
Delete Elements of a Listbox
We can delete selected elements or all elements or by position of the element. You can uncomment the respective line and check how to remove items.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("500x500")
def my_upd():
lb1.delete(tk.ANCHOR) # Delete selected element
#lb1.delete(2) # Remove the 2nd position element
lb1 = tk.Listbox(my_w,height=6)
lb1.grid(row=1,column=1)
my_list=['PHP','Python','MySQL','JavaScript','JQuery']
for element in my_list:
lb1.insert(tk.END,element)
#lb1.delete(0,END) # Delete all elements
b1 = tk.Button(my_w, text="Delete", command=lambda: my_upd())
b1.grid(row=1,column=2)
my_w.mainloop()
We can directly write our delete command with onClick event of the button.
Updating Label with Listbox user selected Option Part III
Here we have connected one StringVar()str1 to the Labellb2. Once the user selects any option, the function my_upd() is triggered and the value of the StringVar()str1 is updated by reading the selected element.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("450x200") # Size of the window
font1=['Arial',15,'normal'] # Font type,size and style
lb1 = tk.Listbox(my_w,height=6,font=font1,bg='lightgreen')
lb1.grid(row=1,column=1,padx=30,pady=20)
my_list=['PHP','Python','MySQL','HTML','JQuery']
for element in my_list:
lb1.insert(tk.END,element)
str1=tk.StringVar(value='plus2net') # common string Variable
lb2=tk.Label(my_w,bg='lightyellow',textvariable=str1,font=font1)
lb2.grid(row=1,column=3,padx=5,pady=20)
def my_upd(my_widget): # Triggers on selection of Listbox
selected_indices = lb1.curselection() #index
str1.set(lb1.get(selected_indices)) # Update StrinvVar()
lb1.bind('<<ListboxSelect>>', my_upd) # binding select event
my_w.mainloop() # Keep the window open
Multiple selection of options of a Listbox
Displaying Multiple selections of options of a Listbox in a Label Part IV
We will modify the above code and add the option selectmode='multiple' to the Listbox. Inside the function my_upd() our variable selected_indices will hold a list of indices of the selected ( multiple ) options. Using this list we will prepare a string and update the StringVar()str1.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("450x200") # Size of the window
font1=['Arial',15,'normal'] # Font type,size and style
lb1 = tk.Listbox(my_w,height=6,selectmode='multiple',
width=10,font=font1,bg='lightgreen')
lb1.grid(row=1,column=1,padx=10,pady=20)
my_list=['PHP','Python','MySQL','HTML','JQuery']
for element in my_list:
lb1.insert(tk.END,element)
str1=tk.StringVar(value='plus2net') # common string variable
lb2=tk.Label(my_w,bg='lightyellow',textvariable=str1,font=font1)
lb2.grid(row=1,column=2,padx=1,pady=20)
def my_upd(my_widget): # Triggers on selection of Listbox
selected_indices = lb1.curselection() #index
#my_list=map(lambda n:lb1.get(n),selected_indices)
my_list=list() # blank list
for i in selected_indices:
#print(lb1.get(i))
my_list.append(lb1.get(i)) # adding to list
my_str=",".join(map(str,my_list)) # convert to string
str1.set(my_str) # Update stringVar()
lb1.bind('<<ListboxSelect>>', my_upd) # binding select event
my_w.mainloop() # Keep the window open
Reading elements from Listbox
We can add elements to Listbox by using listvariable option. Using this same variable we can get all the items of the listbox as Tuple.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("500x500") # Size of the window
my_list=['PHP','Python','MySQL']
my_list_v=tk.Variable(value=my_list)
lb1 = tk.Listbox(my_w,listvariable=my_list_v)
lb1.grid(row=1,column=1)
d1=my_list_v.get() # get all the elements of listbox as tuple.
print(d1)# print all elements
my_w.mainloop() # Keep the window open
Create a button to delete all elements of a Listbox, Another button to delete selected element
Create a Listbox with 10 items. Create a button to delete 2nd element. Check what happens if multiple time the button is clicked.
In first column create a Listbox with PHP, MySQL & Python as elements.
In Second column create three checkbuttons for PHP, MySQL & Python
In third column create three radiobuttons for PHP, MySQL & Python.
On selection ( click ) of PHP ( or any other option ) in first column (having listbox) , respective checkbox ( PHP ) and radiobutton (PHP) should be selected.
Use the CSV file at the end of the dictionary tutorial. Then read the data and populate one listbox with student names. Then on selection of any student name the respective subject marks with attendance should populate. You should continue with printing of total mark and attendance