I have made a simple GUI with a Entry for input, a button to save the input to an ini file. And for the hole purpose with this post is in the listbox I have added. How is it possible to refresh/update the listbox ? If I enter my name and hit save, the file is generated and stored in the folder. But the list dosent update in the GUI.
I want the listbox to show the new files added, when the GUI i open. Mabye a button that updates the GUI ?
enter code here root
root = Tk()
root.geometry('400x300')
L1 = Label(root, text='Input')
L1.place(x=10, y=10)
e1 = Entry(root)
e1.place(x=10, y=40)
def SaveInput():
config = configparser.ConfigParser()
config.add_section("DATA")
config.set("DATA", "NAME", e1.get())
list_files = os.listdir(os.getcwd())
list_numbers = [int(x[:-4]) for x in list_files if x.endswith(".ini")]
if len(list_numbers) != 0:
new_file_num = max(list_numbers) + 1
else:
new_file_num = 1
new_file_name = str(new_file_num) + ".ini"
with open(new_file_name, "w") as file_obj:
config.write(file_obj)
L1 = Listbox(root, height=5, width=50)
L1.place(x=10, y=100)
# LISTBOX
def get_filenames():
path = "C:/Users/ita9bi/Desktop/Test list"
return os.listdir(path)
for filename in get_filenames():
L1.insert(END, filename)
B1 = Button(root, text='Save', command=SaveInput)
B1.place(x=10, y=60)
root.mainloop()
You can either add last saved item to your listbox or re-populate all filenames everytime you press Save button.
def SaveInput():
....
....
new_file_name = str(new_file_num) + ".ini"
L1.insert(END, new_file_name)
or
def SaveInput():
....
....
L1.delete(0, END) #clear listbox
for filename in get_filenames(): #populate listbox again
L1.insert(END, filename)
Instead of re-populating eveytime, adding only last item would be much faster and efficient.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With