'How to add variables into a listbox for GUI in python

Here is my code about adding a book information: ( i make a normal display about book information and some "adding" and "delete" button )

import tkinter
from tkinter import *
from tkinter import END

bt = Tk()
bt.geometry("500x400")

global book_name
global author_name
global type_book
global quantity
global price
global ls1

book_name = StringVar()
author_name = StringVar()
type_book = StringVar()
quantity = StringVar()
price = StringVar()

def record():
    book_name_verify = book_name
    author_name_verify = author_name
    type_book_verify = type_book
    quantity_verify = quantity
    price_verify = price

    ls1.insert(END,book_name_verify,
    author_name_verify,
    type_book_verify,
    quantity_verify,
    price_verify)


name = Label(bt, text = "Book's name") 
name.grid(row = 0, column = 0)

author = Label(bt, text = "Author")
author.grid(row = 1, column = 0)

btype = Label(bt, text = "Book's type")
btype.grid(row = 0, column = 2)

quantity = Label(bt, text = "Quantity")
quantity.grid(row = 1, column = 2)

price = Label(bt, text = "price")
price.grid(row = 2, column = 0)
 
e1 = Entry(bt, textvariable = book_name)
e1.grid(row = 0, column = 1)

e2 = Entry(bt, textvariable = author_name)
e2.grid(row = 1, column = 1)

e3 = Entry(bt, textvariable = type_book)
e3.grid(row = 0, column = 3)

e4 = Entry(bt, textvariable = quantity)
e4.grid(row = 1, column = 3)

e5 = Entry(bt, textvariable = price)
e5.grid(row = 2, column = 1)

ls1 = Listbox(bt, height = 15,width = 60) #list box
ls1.grid(row = 3, column = 0, columnspan= 5)

#Button part
bn1 = Button(bt, text = "Add", width = 12,command=record)
bn1.grid(row = 5, column = 1)

bt.mainloop()

I have a problem that when i try to input the variables into my list box that: enter image description here

Hope you guys can help me to fix this problem



Solution 1:[1]

Bugs

  • Your label names are same as the textvariable names
  • You are not using the textvariable.get() method

Fixes

  • Change all your label names
# Labels
lbl_book_name = Label(bt, text="Book's name") 
lbl_book_name.grid(row = 0, column=0)

lbl_author_name = Label(bt, text="Author")
lbl_author_name.grid(row=1, column=0)

lbl_book_type = Label(bt, text="Book's type")
lbl_book_type.grid(row=0, column=2)

lbl_quantity = Label(bt, text="Quantity")
lbl_quantity.grid(row=1, column=2)

lbl_price = Label(bt, text="price")
lbl_price.grid(row=2, column=0)
  • Use the .get() method to get value of textvariable
def record():
    book_name_verify = book_name.get()
    author_name_verify = author_name.get()
    book_type_verify = book_type.get()
    quantity_verify = quantity.get()
    price_verify = price.get()

    ls1.insert(
        END, book_name_verify, author_name_verify,
        book_type_verify, quantity_verify, price_verify)

Reference

Complete code

After all fixes your complete code will look like:

import tkinter
from tkinter import *

bt = Tk()
bt.geometry("500x400")

global book_name
global author_name
global type_book
global quantity
global price
global ls1

book_name = StringVar()
author_name = StringVar()
book_type = StringVar()
quantity = StringVar()
price = StringVar()

def record():
    book_name_verify = book_name.get()
    author_name_verify = author_name.get()
    book_type_verify = book_type.get()
    quantity_verify = quantity.get()
    price_verify = price.get()

    ls1.insert(
        END, book_name_verify, author_name_verify,
        book_type_verify, quantity_verify, price_verify)

# Labels
lbl_book_name = Label(bt, text="Book's name") 
lbl_book_name.grid(row = 0, column=0)

lbl_author_name = Label(bt, text="Author")
lbl_author_name.grid(row=1, column=0)

lbl_book_type = Label(bt, text="Book's type")
lbl_book_type.grid(row=0, column=2)

lbl_quantity = Label(bt, text="Quantity")
lbl_quantity.grid(row=1, column=2)

lbl_price = Label(bt, text="price")
lbl_price.grid(row=2, column=0)

# Entries
e1 = Entry(bt, textvariable=book_name)
e1.grid(row=0, column=1)

e2 = Entry(bt, textvariable=author_name)
e2.grid(row=1, column=1)

e3 = Entry(bt, textvariable=book_type)
e3.grid(row=0, column=3)

e4 = Entry(bt, textvariable=quantity)
e4.grid(row=1, column=3)

e5 = Entry(bt, textvariable=price)
e5.grid(row=2, column=1)

# list box
ls1 = Listbox(bt, height=15,width=60)
ls1.grid(row=3, column=0, columnspan=5)

# Button
bn1 = Button(bt, text="Add", width=12,command=record)
bn1.grid(row=5, column=1)

bt.mainloop()

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Billy