Home > Enterprise >  Tkinter fails to pass Entry data between two functions
Tkinter fails to pass Entry data between two functions

Time:03-29

When I run the following code, I receive the following error: NameError: name 'barE' is not defined.

Does Tkinter's Entry work from one function to another? I have a global window and frame in this program and it passes the user's input without error using the same syntax as below.

def newSearchForm():
    window2=Tk()
    window2.geometry("650x400")
    frame = Frame(window2)
    frame.pack(side="top", expand=True, fill="both")
    
    barcode=Label(frame,text="Please Enter The Barcode", font='time 15')
    barcode.place(x=10,y=90)
    barE=Entry(frame)
    barE.place(x=250,y=90)

    isbn=Label(frame,text="Please Enter The ISBN", font='time 15')
    isbn.place(x=10,y=130)
    isbE=Entry(frame)
    isbE.place(x=250,y=130)
    
    repeatSearchButton=Button(frame,text="Enter", command=newSearch,width=12,bg='gray')
    repeatSearchButton.place(x=150,y=170)
    
    window.mainloop()

def newSearch():
    uB = barE.get()
    uI = isbE.get()
    carlSearch(uB, uI)
    itemTitle=workspace.find_element_by_xpath('//*[@id="mainContent"]/div[2]/div[2]/div[2]/div[1]/div[1]/div').text
    ingramSearch()
    fantasticFictionSearch(itemTitle)
    outputMetadata()

I tried using the lambda command to explicitly pass the variables and that didn't work. I tried using anexising window and that didn't work, so I destroyed it and created a new one.

CodePudding user response:

I would suggest you to use text variable. You can create a text variable by StringVar and assign it in the entry widget.

For more details on why to use text variable see Should I use Entry's .get() or its textvariable's for Tkinter in Python?

Usage:

barE_var=StringVar()
barE=Entry(frame,textvariable=barE_var)
barE.place(x=250,y=90)

To get value use barE_var.get().

Does Tkinter's Entry work from one function to another?

It does work, but have to use global keyword to access them.

def newSearch():
    global barE_var
  • Related