Removing Selected Items in ListBox - The Advantages of Learning Python Programming for Beginners: A Comprehensive Guide

Latest

Dive into the world of Python programming with our comprehensive resources

Music

logo

Monday, January 23, 2023

Removing Selected Items in ListBox

 

In this article, we will remove the items we have selected in the listbox. Listbox structure is given below.

 

lbox= Listbox(root, selectmode= MULTIPLE,bd=1, width=70,height=12, font=('Times', 14))

 

In order to select the items in the listbox, we must make the selectmode feature multiple.

 


 

 

 

 

 

 

 

 

Figure 1

 


 

 

 

 

 

 

Figure 2

 


 

 

 

 

 

 

 

 

Figure 3

 

#Import the required libraries.

from tkinter import *

from tkinter import messagebox

import tkinter as tk

 

#Create an instance of tkinter frame or window.

root= Tk()

root.title("msssql db example...bs")

#Set the geometry.

root.geometry("800x500")

 

#Create a text Label.

label= Label(root, text="Select items from the list..." +"\n" + "Listeden item seciniz...", font= ('Poppins bold', 18))

label.pack(pady= 20)

 

#Define the function.

def delete_item():

 

   MsgBox = tk.messagebox.askquestion ('Delete Items','Are you sure you want to delete item(s)'+'\n'+"Secili itemsleri silmek istediginizden emin misiniz?",icon = 'warning')

   if MsgBox == 'yes':

 

    selected_item = lbox.curselection()

   for item in selected_item[::-1]:

    lbox.delete(item)

   else:

    tk.messagebox.showinfo('Return','You will now return to the application screen'+'\n'+"Uygulama ekranina geri doneceksiniz",icon = 'info')

 

lbox= Listbox(root, selectmode= MULTIPLE,bd=1, width=70,height=12, font=('Times', 14))

lbox.pack()

 

#add items.

items=[

 '1  Bahadir  Sahin    basahin@hotmail.com',

 '2  Melissa  Parker   melparker@gmail.com',

 '3  Tommy    Paton    tommypat@hotmail.com',

 '4  Alice    May      alicemay12@hotmail.com',

 '5  Michael  Alon     michaelal57@gmail.com',

 '6  Mike     Carson   mikecarson33@hotmail.com',

 '7  Tracy    Abbot    tracyabbot82@hotmail.com',

 '8  Nancy    White    nancywh@gmail.com',

 '9  Jack     Fox      jackfox89@hotmail.com',

 '10 Alina    Benson   alinabens2@hotmail.com',

 '11 Joe      Eagle    joeeagle7@gmail.com',

 '12 Johanna  Manson   johannamans@hotmail.com',

 '13 Emilia   Peterson emiliapets@hotmail.com'

  ]

 

#Now iterate over the list.

for item in items:

   lbox.insert(END,item)

 

#Create a button to remove the selected items in the list.

 

Button(root, text= "Delete (Sil)",width=30,height=2,font='arial 14 bold', command= delete_item).pack(pady= 20)

 

#Keep running the window.

root.mainloop()

 

I have come to the end of this article. See you in my next article. Happy coding. Bahadir Sahin

No comments:

Post a Comment

Featured post

Python Introduction

What is Python? Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built ...