Why Is Tkinter Entry's Get Function Returning Nothing

Why is Tkinter Entry's get function returning nothing?

It looks like you may be confused as to when commands are run. In your example, you are calling the get method before the GUI has a chance to be displayed on the screen (which happens after you call mainloop.

Try adding a button that calls the get method. This is much easier if you write your application as a class. For example:

import tkinter as tk

class SampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.entry = tk.Entry(self)
self.button = tk.Button(self, text="Get", command=self.on_button)
self.button.pack()
self.entry.pack()

def on_button(self):
print(self.entry.get())

app = SampleApp()
app.mainloop()

Run the program, type into the entry widget, then click on the button.

Tkinter Entry's get function returns nothing

The function prints nothing beacause you have changed the message value in the current function where the entry box is defined.

So, when you write v.get(), it generally returns an empty text.The message variable needs to call every time when submit button is pressed. Hence, message variable should be changed inside submit_f() function.

Here's the Solution,

import tkinter
from tkinter import *

message = ''
# start_chatting function
def start_chatting ():
global v
master2 = Tk()
master2.geometry("1280x720")
master2.title("Messenger")
label = Label(master2, text = "Messenger!!!",bg = '#1e00ff',fg ='yellow',width = 35, height = 5).place(x = 500, y = 0)
username_label = Label(master2,text = usernames[position_counter],bg = '#91806d',fg ='white',width = 10, height = 2).place(x = 0, y = 100)
L1 = Label(master2, text = "Type your message : ").place(x=0, y = 680)
v = StringVar()
e = Entry(master2,textvariable = v)
e.insert(END, '')
e.pack()
e.place(x = 115, y = 680)
submit_button = Button(master2,text = "Submit",command = submit_f).place(x = 200, y = 680)
master2.mainloop()

#submit_f function
def submit_f ():
global message
message = message + " " + v.get()
print(message)

start_chatting()

Tkinter returning .!entry instead of text contents of the Entry

That's probably because in the input function, you are printing

print(e1)
print(e2)

which are the Entry objects. You might have been trying to print the title and body, which are being extracted, but unused.

Tkinter Entry widget is returning an empty list when used in a toplevel window

First, we will understand why you got a empty list: your code is executed sequentially, so when you do the entities.get() you haven't yet written anything nor pressed "Submit", i.e., you want to read the entry box once you press the "Submit", not earlier, for that reason, you have the command = ?.
As I am aware, you have mainly 2 options:

  1. Get the text from the button itself
  2. Create a variable linked to the entry box and read this

Method 1: read the data from the entry

from tkinter import *

root = Tk()
root.title("Welcome screen")
root.geometry("300x300")

def do_stuff(entry):
print(entry.get())

def open_new_window():

top = Toplevel()
top.title("second window")

entities = Entry(top)
entities.pack()
entities.focus_set()

sub_button = Button(top, text="Submit", command= lambda: do_stuff(entities))
sub_button.pack(pady=20)
close_btn = Button(top, text="Close", command=top.destroy)
close_btn.pack()

open_button = Button(root, text="New Window", command=open_new_window)
open_button.pack(pady=20)
exit_button = Button(root, text="Close", command=root.destroy)
exit_button.pack(pady=20)
root.mainloop()

Method 2: link a variable

from tkinter import *

root = Tk()
root.title("Welcome screen")
root.geometry("300x300")

def do_stuff(text_entry):
print(text_entry.get())

def open_new_window():

top = Toplevel()
top.title("second window")

text_entry = StringVar()
entities = Entry(top, textvariable = text_entry)
entities.pack()
entities.focus_set()

sub_button = Button(top, text="Submit", command= lambda: do_stuff(text_entry))
sub_button.pack(pady=20)
close_btn = Button(top, text="Close", command=top.destroy)
close_btn.pack()

open_button = Button(root, text="New Window", command=open_new_window)
open_button.pack(pady=20)
exit_button = Button(root, text="Close", command=root.destroy)
exit_button.pack(pady=20)
root.mainloop()

The main advantage in this last approach is that you can play with the text before and after the entry is built.

What is returned by .get() on an empty Tkinter Entry Widget

if you don't enter anything into your tk.Entry, it will contain a '' by default..

example:

import tkinter as tk
root = tk.Tk()
entry = tk.Entry(root)
entry.pack()
print(repr(entry.get()))
root.mainloop()

output:
''
it is because you didn't enter anything to the entry..

Why is `None` returned instead of tkinter.Entry object?

The place method of Entry doesn't return a value. It acts in-place on an existing Entry variable.



Related Topics



Leave a reply



Submit