how ot specify the element in listbox and problems with if statement

  listbox, listboxitem, python

run this code ,gui is created ,listbox with years and update button is show ,select a year and update, a second listbox is created with the list of movies but the problem i face is ,only first range of movies is inserted in the second list for all years, the problem is in (if statement)

  import csv
  from tkinter import *
  import pyperclip 
  import clipboard
  import webbrowser

  filepath =(r"C:UserssanjaDownloadsProject_Movie.csv")

  File = open(filepath)
  Reader = csv.reader(File)
  Data = list(Reader)
  del(Data[0])

  root = Tk()
  root.geometry('600x500')


  def callback(url):
     webbrowser.open_new(url)

  list_of_years = []
  for x in list(range(0,110,5)):
        list_of_years.append(Data[x][0])
  var = StringVar(value = list_of_years)
  listbox1 = Listbox(root,borderwidth=2,border=2, listvariable = var)
  listbox1.place(x=50,y=10)

  list_of_movies = []

  def update():

      list_of_movies = []
      var1 = StringVar(value = list_of_movies)
      my_listbox = Listbox(borderwidth=2,border=2,width=50, listvariable = var1) 
      my_listbox.place(x=250,y=10)               


      if list_of_years[0]:
         for a in list(range(0,5)):
            list_of_movies.append(Data[a][1])

      elif list_of_years[1]:
         for y in list(range(5,10)):
                  list_of_movies.append(Data[y][1])
            
     elif list_of_years[2]:
         for b in list(range(10,15)):
                  list_of_movies.append(Data[b][1])            
                
     else :
         for u in list(range(105,110)):
                  list_of_movies.append(Data[u][1])   
             
             
     var1 = StringVar(value = list_of_movies)
     my_listbox = Listbox(borderwidth=2,border=2,width=50, listvariable = var1) 
     my_listbox.place(x=250,y=10)             
             
                                                  
button1 = Button(root, text="Update",bg="blue",fg="white",width=6, command=update)
button1.place(x=200,y=360)


root.mainloop()

Source: Python Questions

LEAVE A COMMENT