Reputation: 69
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)
Upvotes: 1
Views: 79
Reputation: 1474
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()
Upvotes: 1
Reputation: 489
You have set v1
to e1.get()
before anything could be entered into the entry.
I tried the following code, and it works fine.
from tkinter import * # SHOULD NOT USE.
F1=Tk()
F2=Tk()
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)
Button (F2,text = "Paid",command=lambda:insertdata(e1.get())).pack(side= TOP) # SHOULD NOT USE.
Upvotes: 0