Search code examples
pythonpython-3.xtkintertkinter-entry

how to get the the value from the entry widget in tkinter in python3.6


I'm trying to get the data from the entry box.I'm not getting the use of those variables. It's showing me blank when I try to print the result. I tried using lambda but still not working. I'm new at this. Please show me where I'm wrong. I tried online but they are older version solutions.

def insertdata(E1):
       print(E1)


e1 = StringVar()

L1 = Label(F1, text ="Serial No:",anchor = E)
L1.grid(row = 0 ,column = 0)

E1  = Entry(F1,textvariable = e1)
E1.grid(row = 0 ,column = 2, sticky = N)
v1 = e1.get()
Button (F2,text = "Paid",command=lambda:insertdata(v1)).pack(side= TOP)

Solution

  • This how to get content in entry widget and print. With the code you posted, you are doing a lot of wrong things; you cannot use pack and grid to postion your widget in the same window. Also never do this: Button (F2,text = "Paid",command=lambda:insertdata(v1)).pack(side= TOP), but always position your layout manager on the next line.

    EXAMPLE

    b = Button (F2,text = "Paid",command=lambda:insertdata(v1))
    b.pack(side= TOP)

    FULL CODE

    from tkinter import *
    
    
    def insertdata():
        print(e1)
        print(E1.get())
    
    
    root = Tk()    
    
    L1 = Label( text="Serial No:", anchor=E)
    L1.grid(row=0, column=0)
    
    e1 = StringVar()
    E1 = Entry( textvariable=e1)
    E1.grid(row=0, column=2, sticky=N)
    
    b = Button( text="Paid", command=insertdata)
    b.grid(row=10, column=30)
    
    root.mainloop()