Search code examples
pythonpandasdataframetkinterlistbox

Listbox in Tkinter: output value based on input by using corresponding Excel columns


I'm aware that my code isn't very clean, , my primary focus at the moment is to make the program work.

I’m working with Tkinter and I created a search- and listbox based on a column in Excel. The Excelfile is imported by pandas, as a dataframe. The idea is that people can search for something (for example ‘Eiffel Tower’), that the value (‘Eiffel Tower’) is selected and that Python gives the construction date as output (so for example the year 1889) in the interface.

You search and make sure that the value is visible in the entrybox, and then you click on a button. After clicking on the button, you will see ‘1889’.

Both the buildings as the construction dates are listed in an Excelfile. Column A contains the buildings, column B contains the construction dates. The search and listbox works. But I’m not ably to connect column A to column B, or to get an output based on the input that the uses gives.

The 'output_Startdate' was to test if the if-statement worked (what it does). The 'def connectie()' is me trying to find a solution.

My code:

import tkinter as tk
from tkinter import *
from tkinter import Listbox
from tkinter import ttk
import pandas as pd

interface = tk.Tk()
interface.configure(bg="#60c1c9")
interface.geometry('1500x750')
interface.title('Construction Dates')


title = Label(interface, text='1. BUILDINGS')
title.configure(bg="#60c1c9", fg="#000000", font=("Calibri", 20, "bold"))
title.place(relx=0.15, rely=0, anchor=N)

file_name = “List_Buildings.xlsx”
xl_workbook = pd.ExcelFile(file_name)
df = xl_workbook.parse(“Buildings”)
alist = df['MONUMENT'].tolist()
Startdate = df['Startdate'].tolist()
Enddate = df['Enddate'].tolist()

Label(
    text="Select what you see on the picture.",
    bg="#60c1c9",
    fg="#000000",
    font=("Calibri", 12)
).place(relx=0.29, rely=0.05, anchor=N)


def update(data):
    my_list_1.delete(0, END)

    for entry in data:
        my_list_1.insert(END, entry)


def check(e):
    typed = entry_1.get()

    if typed == '':
        data = alist
    else:
        data = []
        for item in alist:
            if typed.lower() in item.lower():
                data.append(item)
    update(data)


def fillout(e):
    entry_1.delete(0, END)
    entry_1.insert(0, my_list_1.get(ACTIVE))


entry_1 = Entry(interface, width=53)
entry_1.place(relx=0.205, rely=0.12, anchor=N)
entry_1.bind('<KeyRelease>', check)

my_list_1: Listbox = Listbox(interface, height=20, width=50)
my_list_1.place(relx=0.2, rely=0.15, anchor=N)
my_list_1.bind("<<ListboxSelect>>", fillout)

scrollbar_v = Scrollbar(interface, orient=VERTICAL, command=my_list_1.yview)
scrollbar_v.place(relx=0.301, rely=0.151, height=324)
scrollbar_h = Scrollbar(interface, orient=HORIZONTAL, command=my_list_1.xview)
scrollbar_h.place(relx=0.0985, rely=0.583, width=320.5)


#alist = df['MONUMENT'].tolist()
#output = df['Startdate'].tolist()
#df2 = pd.DataFrame(columns=['MONUMENT', 'Startdate', 'Enddate'])
#df2 = df.apply(lambda x: df['MONUMENT'] == df['Startdate'])
#print(df2)

def connectie():
    value = entry_1.get()
    for i in df['MONUMENT']:
        if value == alist:
            BLOCK_NAME.set(output)
            return

def output_Startdate():
    if entry_1.get() == ‘Eiffeltower’:
        tekst = tk.Label(interface, text="good")
        tekst.place(relx=0.3, rely=0.8)
    else:
        tekst = tk.Label(interface, text="this value doesn't excist")
        tekst.place(relx=0.3, rely=0.8)


button = Button(interface, text='click here', command=output_Startdate)
button.place(relx=0.29, rely=0.7)


interface.mainloop()

Solution

  • I'm not sure what your data looks like (you didn't hand us a sample), so I hope I did it right. There are two parts to my answer, the first is for loading the file and setting the index column (I hope the names are all unique), and the second part is how to loc for the data you are looking for.

    file_name = 'List_Buildings.xlsx'  # file name
    # read the file's Sheet1 and create dataframe with column 'MONUMENT' as index
    df = pd.read_excel(file_name, 'Sheet1', index_col='MONUMENT')
    # create alist from the index
    alist = df.index.tolist()
    
    
    def output_Startdate():
        # get the entry_1 value
        monument = entry_1.get()
        # use monument (the entry_1 value) as index for dataframe loc and 'Startdate' as the column
        start_date = df.loc[monument, 'Startdate']
        # set the text for the label
        tekst = tk.Label(interface, text=f"Start date: {start_date}")
        tekst.place(relx=0.3, rely=0.8)