Я пытаюсь создать раскрывающееся меню, которое использует содержимое текстового файла, в который программа записывает, чтобы заполнить свой выбор. Моя проблема заключается в том, что при добавлении в него текстового файла эта новая строка не добавляется в варианты выпадающего меню.
Я попытался использовать словарь вместо списка, который я пытался добавить в список, и я подумал о том, чтобы уничтожить основной цикл, а затем вызвать его, но это кажется чрезмерным.
#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)
Код в конце, который распечатывает новый добавленный список, включающий новый элемент, но виджет не открывается с новым элементом