Python tkinter Listbox solution

Listbox Tutorial & Exercise
By using a text box add elements to a Listbox
import tkinter as tk
from tkinter import BOTH, END, LEFT
my_w=tk.Tk()
my_w.geometry('300x200')

def my_upd():
    #print(t1.get("1.0",END)) # To print the entered data
    l2.insert(tk.END,t1.get("1.0",END))
    t1.delete("1.0",END)
    t1.update                 # Empty the text box 
    
l1=tk.Label(my_w,text="Enter your text",width=10)
l1.grid(row=1,column=1)
t1=tk.Text(my_w,height=1,width=8,bg='yellow')
t1.grid(row=1,column=2)
b1 = tk.Button(my_w, text='Update', width=10,bg='red',command=lambda: my_upd()) # button added
b1.grid(row=1,column=3) 

l2=tk.Listbox(my_w)

l2.grid(row=2,column=2)
a=t1.get("1.0",END)
my_w.mainloop()
Create a Listbox with 10 items. Create a button to delete 2nd element. Check what happens if multiple time the button is clicked.
import tkinter as tk
from tkinter import BOTH, END, LEFT, ANCHOR
my_w=tk.Tk()
my_w.geometry("500x500")
def my_upd():
    l1.delete(2)
def my_fun():
    l1.delete(0,END)

l1=tk.Listbox(my_w,width=6)
l1.grid(row=1,column=1, rowspan=8)
my_list=['PHP','Python','MySQL','Java','HTML','C','C++',"ASP",'.NET']
for i in my_list:
    l1.insert(tk.END,i)
    
b1=tk.Button(my_w,text="Delete",command=lambda: my_upd())
b1.grid(row=1,column=2)

b2=tk.Button(my_w,text="Delete All",command=lambda: my_fun())
b2.grid(row=1,column=3)

my_w.mainloop()
Create a button to delete all elements of a Listbox, Another button to delete selected element
import tkinter as tk
from tkinter import BOTH, END, LEFT, ANCHOR
my_w=tk.Tk()
my_w.geometry("500x500")
def my_upd():
    l1.delete(1)
def my_fun():
    l1.delete(0,END)

l1=tk.Listbox(my_w,width=6)
l1.grid(row=1,column=1, rowspan=8)
my_list=['PHP','Python','MySQL','Java','HTML','C','C++',"ASP",'.NET']
for i in my_list:
    l1.insert(tk.END,i)
    
b1=tk.Button(my_w,text="Delete",command=lambda: my_upd())
b1.grid(row=1,column=2)

b2=tk.Button(my_w,text="Delete All",command=lambda: my_fun())
b2.grid(row=1,column=3)

my_w.mainloop()
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.
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)
    if(value=="PHP"):
     r1_v.set(1)
     c1_v1.set(1)
     c2_v1.set(0)
     c3_v1.set(0)
    
    elif(value=="Python"):
     r1_v.set(2)
     c2_v1.set(1)
     c1_v1.set(0)
     c3_v1.set(0)
        
    elif(value=="MySQL"):
     r1_v.set(3)
     c3_v1.set(1)
     c1_v1.set(0)
     c2_v1.set(0)
    
    
l1 = tk.Listbox(my_w,height=4)
l1.grid(row=1,column=1,rowspan=3) 
my_list=['PHP','Python','MySQL']
for element in my_list:
    l1.insert(tk.END,element)
r1_v=tk.IntVar()    
r1 = tk.Radiobutton(my_w, text='PHP', variable=r1_v, value=1)
r1.grid(row=1,column=2)

r2 = tk.Radiobutton(my_w, text='Python', variable=r1_v, value=2)
r2.grid(row=2,column=2)

r3 = tk.Radiobutton(my_w, text='MySQL', variable=r1_v, value=3)
r3.grid(row=3,column=2)

c1_v1=tk.IntVar()
c1 = tk.Checkbutton(my_w, text='PHP', variable=c1_v1,onvalue=1,offvalue=0,command=my_upd)
c1.grid(row=1,column=3)

c2_v1=tk.IntVar()
c2 = tk.Checkbutton(my_w, text='Python', variable=c2_v1,onvalue=1,offvalue=0,command=my_upd)
c2.grid(row=2,column=3)

c3_v1=tk.IntVar()
c3 = tk.Checkbutton(my_w, text='MySQL', variable=c3_v1,onvalue=1,offvalue=0,command=my_upd)
c3.grid(row=3,column=3)

    
l1.bind('<>', my_upd)
    
my_w.mainloop()  # Keep the window open
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
import tkinter as tk 
from tkinter import BOTH, END, LEFT
fob=open('student.csv','r')
my_dict={}
for i in fob:
 #print(i)
 my_list=i.split(',')
 my_list[-1]=my_list[-1].strip()   
 #print(my_list[0])
 my_dict.update({my_list[1]:[my_list[0],my_list[2],my_list[3],my_list[4],my_list[5]]})
 my_list.clear()   
#print(my_dict)
#print(my_dict[1][2])
my_window = tk.Tk()
#student_id='Alex'

def my_show(my_widget):
    my_w = my_widget.widget
    index = int(my_w.curselection()[0])
    value = my_w.get(index)
    #print ("You selected item ",index, value) 
    student_id=value
    t1.delete('1.0',END)       # Delete from postion 0 till end 
    t1.update()
    t2.delete('1.0',END)       # Delete from postion 0 till end 
    t2.update()
    t3.delete('1.0',END)       # Delete from postion 0 till end 
    t3.update()
    t4.delete('1.0',END)       # Delete from postion 0 till end 
    t4.update()
    t1.insert("1.0",my_dict[student_id][1])
    t2.insert("1.0",my_dict[student_id][2])
    t3.insert("1.0",my_dict[student_id][3])
    t4.insert("1.0",my_dict[student_id][4])

    my_str_n.set(student_id)
###########
list1 = tk.Listbox(my_window,height=4)
list1.grid(row=10,column=2) 
#my_list=['PHP','Python','MySQL']
for element in my_dict:
    list1.insert(tk.END,element)
    
list1.bind('<>', my_show)
###########
def my_upd():
    my_str1=t1.get("1.0",'end-1c')
    my_str2=t2.get("1.0",'end-1c')
    my_str3=t3.get("1.0",'end-1c')
    my_str4=t4.get("1.0",'end-1c')
    my_str=int(my_str1)+int(my_str2)+int(my_str3)
    my_str=str(my_str)
    label5.config(text='Sum : ' + my_str)
    
    my_str4=str((int(my_str4)/200)*100)
    label6.config(text="Attendance in %: " + my_str4)

    

my_window.geometry("400x300")
my_str = tk.StringVar()
my_str_n = tk.StringVar()
my_font=('times', 20, 'bold')
my_font_n=('times', 12)
label = tk.Label( my_window, textvariable=my_str, bg='red',font=my_font )
my_str.set("Welcome to ABC College")
#label.pack()
label.grid(row=1,column=2,columnspan=3)

label_n = tk.Label( my_window, textvariable=my_str_n, bg='yellow',font=my_font_n )
label_n.grid(row=2,column=2)

label1 = tk.Label( my_window, text='Physics' )
label1.grid(row=3,column=1)
t1 = tk.Text(my_window, height=1, width=5)
t1.grid(row=3,column=2)

label2 = tk.Label( my_window, text='Chemistry' )
label2.grid(row=4,column=1)
t2 = tk.Text(my_window, height=1, width=5)
t2.grid(row=4,column=2)

label3 = tk.Label( my_window, text='Math' )
label3.grid(row=5,column=1)
t3 = tk.Text(my_window, height=1, width=5)
t3.grid(row=5,column=2)

label4 = tk.Label( my_window, text='Attendance' )
label4.grid(row=4,column=3)
t4 = tk.Text(my_window, height=1, width=5)
t4.grid(row=4,column=4)

t1.insert("1.0",my_dict[student_id][1])
t2.insert("1.0",my_dict[student_id][2])
t3.insert("1.0",my_dict[student_id][3])
t4.insert("1.0",my_dict[student_id][4])

my_str_n.set(student_id)

button1 = tk.Button(my_window, text='Update',bg='yellow', width=15,command=lambda: my_upd())
button1.grid(row=6,column=3) 

label5 = tk.Label( my_window, text='Sum :' )
label5.grid(row=9,column=1)

label6 = tk.Label( my_window, text='Attendance in % :' )
label6.grid(row=9,column=3)

my_window.mainloop()



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