How to insert items to a tkinter listbox from a grib file without duplicates?

103 views Asked by At

I'm trying to insert items to a tkinter listbox from a grib file using keywords like name or forecastTime, but I don't want the listbox to contain duplicates just one of each value. Couldn't get it to work using lists and ifs only work for specific files and I want it to be operational with any grib I attach to it. Here's the code:

from tkinter import *
import pygrib

okno = Tk()

grbs = pygrib.open('/home/michal/Desktop/ROUTING_subarea_1_wind_wave.grb2')

listaprognoza = Listbox(okno, height=8, width=50, exportselection=False)
listaprognoza.pack()

grbs.seek(0)
for grb in grbs:
    listaprognoza.insert(END, grb.name)
1

There are 1 answers

3
toyota Supra On

but I don't want the listbox to contain duplicates just one of each value

I never tried pygrib

Try this if it can work.

for grb in grbs:
    listaprognoza.insert(END, grb.name)
sort_list()


def sort_list():
    """
    function to sort listbox items case insensitive
    """
    temp_list = list(listaprognoza.get(0, ttk.tkinter.END))
    temp_list.sort(key=str.lower)
    # delete contents of present listbox
    listaprognoza.delete(0, ttk.tkinter.END)
    # load listbox with sorted data
    for item in temp_list:
        listaprognoza.insert(ttk.tkinter.END, item)