LaVOZs

The World’s Largest Online Community for Developers

'; python 3.x - Updating a drop down menu when its choices are from a text file's contents - LavOzs.Com

I am trying to create a drop down menu that uses the contents of a text file, which the program writes to, to populate its choices. My problem lies that when the text file is added to it does not add this new line to the drop down menu's choices.

Ive tried using a dictionary instead of a list Ive tried appended to the list and ive considered destroying the main loop then recalling it but this seems excessive.

#This code does not work as is: the full code includes several widgets not needed to show the problem

import tkinter as tk
from tkcalendar import Calendar, DateEntry
from tkinter import *

root = tk.Tk()  
root.title("Shopping List")
root.configure(background="cornsilk2")
root.geometry("600x500")

Food_list = tk.Frame(root)
Food_list.grid(row=1, column=0)

Date_list = tk.Frame(root)
Date_list.grid(row=0, column=0)

#^^^^^^^^^^^^^basic tkinter stuff and imports^^^^^

DN = open("Dinner_names.txt", "r")
content = DN.readlines()
i = 0
choices = [] #Creates an empty list
for i, line in enumerate(content):
   content = [x.strip("\n") for x in content]
   choices.append([content[i]]) # appends each line
print(content)# Shows the contents list has generated correctly

#^^^^^^^^^Creates the options of drop down menu from the text^^^^^
 def update():
    Recipe = Recipe_entry.get() #Gets the new recipe's name 
    DN = open("Dinner_names.txt", "a") #Opens the text file
    Recipe_str = str(Recipe) #Converts the recipes name to a string
    DN.write(Recipe_str +'\n') #Writes the str to a newline of the file 
    global choices

    DN = open("Dinner_names.txt", "r") #This prints out the new list for 
                                        testing purposes
    Choices_str = str(DN.read())
    print(Choices_str)
    choices = [Choices_str]
    Add_Item.destroy()
 #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 #one of the menu widgets that uses the list
  Menu_1 = OptionMenu(Date_list, tkvar, *choices)
  Menu_1.grid(row=0, column=1)
 #The recipe.entry
     Recipe_entry = Entry(Add_Item)

The code at the end that prints out the new appended list which included the new item but the widget does not open with the new item

Related
Addition of rows - grid Tkinter. Individual commands for OptionMenu
How do I populate tkinter drop-down box with data retrieved from my SQLite database?
TKinter OptionMenu: How to get the selected choice?
python tkinter: use a drop-down menu to enable a text field
Python gui table whose cells are drop down lists with dynamic content based on the content of other drop-down-list-cells of the same gui-table
tkinter drop down menu from excel
Python tkinter: Getting the number of entries in an OptionMenu dropdown list
tkinter, print text when hovering over OptionMenu objects