0
votes

I'm trying to use an Entry widget to get the user's data and then print it. Why is Tkinter Entry's get function returning nothing? This didn't help me.

This is my code

message = ''
    # start_chatting function
    def start_chatting ():
        global message
        master2 = tk.Tk()
        master2.geometry("1280x720")
        master2.title("Messenger")
        label = tk.Label(master2, text = "Messenger!!!",bg = '#1e00ff',fg ='yellow',width = 35, height = 5).place(x = 500, y = 0)
        username_label = tk.Label(master2,text = usernames[position_counter],bg = '#91806d',fg ='white',width = 10, height = 2).place(x = 0, y = 100)
        v = StringVar()
        L1 = Label(master2, text = "Type your message : ").place(x=0, y = 680)
        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)
        message = message+ v.get()
        master2.mainloop()

#submit_f function  
def submit_f ():
    global message
    print(message)

Keep in mind that this is a part of my code and not all of it.
Thanks in advance!

1
e.get() is one thing, v.get() is apparently a different beast. In your code you're calling StringVar's get method, not Entry's - ForceBru
I tried e.get() but i got the same result. - Nikos Allamanis
Well, if the only code that attempts to retrieve the value is message = message+ v.get(), then this will be executed even before the GUI appears, so you won't even have a chance to input anything. Your submit_f should be getting data from the Entry each time you press the button - ForceBru
The indentation in the code looks broken. - Bryan Oakley

1 Answers

1
votes

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()