Python tkinter Listbox

Displaying a listbox with three elements ( PHP, Python,MySQL).
Basics of Python Tkinter
Tkinter window Listbox
Code is here
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("400x300")  # Size of the window 
my_w.title("plus2net.com")

l1 = tk.Listbox(my_w,height=3)
l1.grid(row=1,column=1) 

l1.insert(1,'PHP')   # Adding one element to Listbox 
l1.insert(2,'Python')
l1.insert(3,'MySQL')

my_w.mainloop()  # Keep the window open
Index of Listbox elements starts from 0 .
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("500x500")  # Size of the window 

l1 = tk.Listbox(my_w)
l1.grid(row=1,column=1) 

l1.insert(1,'PHP')   # Adding one element to Listbox 
l1.insert(2,'Python')
l1.insert(3,'MySQL')

print(l1.get(0))  # Output PHP
print(l1.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("500x500")  # Size of the window 

l1 = tk.Listbox(my_w)
l1.grid(row=1,column=1) 
my_list=['PHP','Python','MySQL']
for element in my_list:
    l1.insert(tk.END,element)
print(l1.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(l1.get(tk.ACTIVE))     # The selected element  
        
l1 = tk.Listbox(my_w,height=4)
l1.grid(row=1,column=1) 
my_list=['PHP','Python','MySQL']
for element in my_list:
    l1.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) 
    
    
l1 = tk.Listbox(my_w,height=4)
l1.grid(row=1,column=1) 
my_list=['PHP','Python','MySQL']
for element in my_list:
    l1.insert(tk.END,element)
    
l1.bind('<<ListboxSelect>>', my_upd)
    
my_w.mainloop()  # Keep the window open

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.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("500x500") 

def my_upd():
    l1.delete(ANCHOR)  # Delete selected element
    #l1.delete(2)  # Delete the 2nd position element 
        
l1 = tk.Listbox(my_w,height=6)
l1.grid(row=1,column=1) 
my_list=['PHP','Python','MySQL','JavaScript','JQuery']
for element in my_list:
    l1.insert(tk.END,element)

#l1.delete(0,END)     # Delete all elements 

#b1 = tk.Button(my_w, text="Delete", command=lambda l1=l1: l1.delete(ANCHOR))  #short method
b1 = tk.Button(my_w, text="Delete", command=lambda: my_upd())
b1.grid(row=1,column=2) 
    
my_w.mainloop()
bgBackground colour of the Listbox
l1 = tk.Listbox(my_w,height=3,bg='yellow')
bdBorder width of the Listbox
l1 = tk.Listbox(my_w,height=3,bd=5)
cursor cursor that will appear once the mouse is over the text box
l1 = tk.Listbox(my_w,height=3,cursor='boat')
You can get a list of cursor shapes in our Tkinter button page
fontFont size style of the button
import tkinter as tk
my_w=tk.Tk()
my_w.geometry('200x100')
    
l1 = tk.Listbox(my_w,height=3,font=18)
b1.grid(row=1,column=1)
my_w.mainloop()
We can use variable for type and style.
import tkinter as tk
my_w=tk.Tk()
my_w.geometry('200x100')
my_font=('times', 8, 'underline')    
l1 = tk.Listbox(my_w,height=3,font=my_font)
b1.grid(row=1,column=1)
my_w.mainloop()
fgSame as foreground
l1 = tk.Listbox(my_w,height=3,fg='green')
heightHeight of the Listbox
l1 = tk.Listbox(my_w, height=3)
Highlight Listbox
highlightcolor : Border colour when the Listbox is in focus.
l1 = tk.Listbox(my_w,height=3,highlightcolor='yellow',highlightthickness=5)
highlightthickness : Thickness of selected Listbox border
l1 = tk.Listbox(my_w,height=3,highlightcolor='yellow',highlightthickness=5)
Relief Listbox
reliefThe Listbox ( borders ) 3 D effect style.
It can take these values raised , sunken ,flat, ridge, solid & groove
#relief  
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("200x400") 

l1 = tk.Label(my_w,  text='relief' ) 
l1.grid(row=1,column=1) 

l1 = tk.Label(my_w,  text='raised' ) 
l1.grid(row=2,column=1)
l1 = tk.Listbox(my_w,height=3,relief='raised')
l1.grid(row=2,column=2) 

l1.insert(1,'PHP')  
l1.insert(2,'Python')
l1.insert(3,'MySQL')

l1 = tk.Label(my_w,  text='sunken' ) 
l1.grid(row=3,column=1)
l2 = tk.Listbox(my_w,height=3,relief='sunken')
l2.grid(row=3,column=2) 

l2.insert(1,'PHP')  
l2.insert(2,'Python')
l2.insert(3,'MySQL')

l1 = tk.Label(my_w,  text='flat' ) 
l1.grid(row=4,column=1)
l3 = tk.Listbox(my_w,height=3,relief='flat')
l3.grid(row=4,column=2) 


l3.insert(1,'PHP')  
l3.insert(2,'Python')
l3.insert(3,'MySQL')

l1 = tk.Label(my_w,  text='ridge' ) 
l1.grid(row=5,column=1)
l4 = tk.Listbox(my_w,height=3,relief='ridge')
l4.grid(row=5,column=2) 

l4.insert(1,'PHP')  
l4.insert(2,'Python')
l4.insert(3,'MySQL')

l1 = tk.Label(my_w,  text='solid' ) 
l1.grid(row=6,column=1)
l5 = tk.Listbox(my_w,height=3,relief='solid')
l5.grid(row=6,column=2) 

l5.insert(1,'PHP')  
l5.insert(2,'Python')
l5.insert(3,'MySQL')

l1 = tk.Label(my_w,  text='groove' ) 
l1.grid(row=7,column=1)
l6 = tk.Listbox(my_w,height=3,relief='groove')
l6.grid(row=7,column=2) 

l6.insert(1,'PHP')  
l6.insert(2,'Python')
l6.insert(3,'MySQL')

my_w.mainloop() 
selectbackground:Background colour once element is selected.
l1 = tk.Listbox(my_w,height=3, selectbackground='yellow')
selectforeground :Foreground (font ) colour once element is selected.
l1 = tk.Listbox(my_w,height=3, selectforeground='green')
selectmode: It can be SINGLE, BROWSE, MULTIPLE, EXTENDED
l1 = tk.Listbox(my_w,height=3,selectmode='multiple')
statevalues= normal, active or disabled
l1 = tk.Listbox(my_w,height=3,state='disable')
widthwidth of the button in Chars
l1 = tk.Listbox(my_w,height=3,width=7)
  1. Exercise on Listbox
  2. By using a text box add elements to a Listbox
  3. Create a button to delete all elements of a Listbox, Another button to delete selected element
  4. Create a Listbox with 10 items. Create a button to delete 2nd element. Check what happens if multiple time the button is clicked.
  5. 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.
  6. 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
    Student Project V4
Solution


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer