-1

When I enter a value into the entry it then should print to the console, however, instead it prints a new line and nothing else. I'm new to tkinter and am just testing things out at the moment.

def main(self, event= None):
    v = StringVar()
    entry1 = Entry(width = 20, textvariable = v)
    entry1.bind("<Return>", self.main)
    entry1.focus()
    self.removeWidgits()
    homeLabel.place_forget()
    exitButton.pack(side = BOTTOM)
    entry1.place(x = 0, y = 0)
    v = entry1.get()
    print(v)
penguin.m
  • 1
  • 1
  • Possible duplicate of [Why is Tkinter Entry's get function returning nothing?](https://stackoverflow.com/questions/10727131/why-is-tkinter-entrys-get-function-returning-nothing) – Ryan Schaefer Sep 12 '18 at 19:08
  • 3
    You're calling `entry1.get()` about a millisecond after you create the widget. You would have to be able to type _really_ fast to enter something before the print statement. – Bryan Oakley Sep 12 '18 at 19:11
  • I can see a couple problems here. It appears you are using a class method. That said you are trying to create an Entry field without passing its parent container. Instead you should create the entry field outside of the method in the `__init__` method most likely and then just get the entry info with a print statement. – Mike - SMT Sep 12 '18 at 19:19
  • The code you have provided is not enough to test your code. What you have provided does not make much sense without some more context. I would never build a method that looks like this because it not very efficient and relatively redundant. – Mike - SMT Sep 12 '18 at 19:26

1 Answers1

0

From your code, it seems that you want to print text in the entry when Return is pressed. Here is a copy of your code that prints the text on pressing enter.

from tkinter import *

root = Tk()
v = StringVar()
entry1 = Entry(width = 20, textvariable = v)
entry1.bind("<Return>", lambda _:print(entry1.get()))
entry1.focus()
entry1.place(x = 0, y = 0)

root.mainloop()
Nouman
  • 6,947
  • 7
  • 32
  • 60