I have python GUI code which creates list of indices. There is two buttons "Add" and "Remove", by clicking "Add" it adds the value from the entry widget and append into the index_list list, similarly with the "Remove" widget. I want to use this generated list in other code. see the code...
from tkinter import *
master = Tk()
Label(master, text='Add_index').grid(row=0,column=0)
var = IntVar()
INDEX = Entry(master, width=10, textvariable=var) #Entry widget with int variable
# widget is not packed yet
Functions
index_list = []
def add_input():
global INDEX
INDEX.grid(row=1,column=0, sticky=E,padx=40,pady=3)
index_list.append(int(INDEX.get()))
Label(master, text=f"Index {INDEX.get()} added, full list {index_list}").grid(row=1,column=0)
#here it displays the list correctly directly into the GUI window
def remove_input():
global INDEX
#global index_list
INDEX.grid(row=1,column=0, sticky=E,padx=40,pady=3)
index_list.remove(int(INDEX.get()))
Label(master, text=f"Index {INDEX.get()} removed, full list {index_list}").grid(row=1,column=1)
Adding button to the Functions
b1 = Button(master,text='Add', command=add_input)
b1.grid(row=2,column=0, sticky=W,padx=25,pady=3)
b2 = Button(master,text='Remove', command=remove_input)
b2.grid(row=2,column=1, sticky=W,padx=25,pady=3)
master.mainloop()
I want the generated list variable (index_list) out of the function, I used return value by using all the code in one function, I also tried using class, also declared it global, but nothing works. I still have not used listbox widget because I thought it doesn't necessary. Any help...