Im having difficulties with deleting content by a function when the entry was made by another function.
def recipe_add():
recepty_add_window = Toplevel()
recepty_add_window.geometry("400x400")
name_entry_label=Label(recepty_add_window, text="Name:").grid(row=1,column=0)
name_entry = Entry(recepty_add_window, width = 50, textvariable=name_var).grid(row=1, column=1,padx=5)
b_add = Button(recepty_add_window, text="Add recipe", width=50, command=add_recipe).grid(row=6, columnspan=2, pady=5, padx = 5)
The add_recipe has name_entry.delete function inside.
def add_recipe():
name_entry.delete(0,END)
I receive: NameError: name 'name_entry' is not defined.
I tried to make name_entry global or changing the Toplevel to "single window app" by forgeting everythin that is a grid and putting it there again, nothing seems to help. Thank you all for any help.
CodePudding user response:
You need to declare a global variable where you define it, not where you use it. Also, .grid()
returns None
, so you first want to store the Entry in the variable and then call grid()
:
from tkinter import *
def recipe_add():
global name_entry
name_var = StringVar()
name_entry = Entry(recepty_add_window, width = 50, textvariable=name_var)
name_entry.grid(row=1, column=1,padx=5)
def add_recipe():
name_entry.delete(0,END)