Python Tkinter program to add an item to Listbox


In this program, you will learn how to add an item to Tkinter Listbox in Python.


 self.listbox.insert(Test.index, self.e1.get())
 Test.index = Test.index + 1

Example: How to add an item to Tkinter Listbox in Python

from tkinter import *


class Test:
    index = 0

    def __init__(self, tk):
        self.l1 = Label(tk, text="Your Favourite Programming language")
        self.l1.place(x=100, y=10)
        self.listbox = Listbox(tk)
        self.listbox.place(x=110, y=100)
        self.e1 = Entry(tk)
        self.e1.place(x=350, y=90)
        self.b1 = Button(tk, text="Add to Listbox", command=self.addItem).place(x=350, y=250)

    def addItem(self):
        self.listbox.insert(Test.index, self.e1.get())
        Test.index = Test.index + 1


tk = Tk()
tk.title("Xiith.com")
tk.geometry("600x500")
tt = Test(tk)
tk.mainloop()

Output:

tkinter listbox add item