1

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)
Arshad_221b
  • 69
  • 2
  • 13

2 Answers2

1

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()
AD WAN
  • 1,414
  • 2
  • 15
  • 28
0

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.
Programmer S
  • 429
  • 7
  • 21
  • Nope. It's still showing the blank space. I'm not able to understand the use of `E1` and `e1` in my code. `E1.get()` is working fine for me `Button (F2,text = "Paid",command= lambda:insertdata(E1.get())).pack(side= TOP)` like this. – Arshad_221b Jun 18 '18 at 10:26
  • @geekarshad Really? I was able to get an output just fine when I tried that solution. I'll edit to include the full code. – Programmer S Jun 18 '18 at 10:31