Python - Assign the value of an entry to a variable

0

I tried to use the command mensagem = str(entry.get()) but it does not work, I tried to use entry = Entry(root, textvariable='mensagem') but it also does not work, see the whole code basically I want to type something in an entry and start in the console ..

from tkinter import *
mensagem = str
def enviar():
    mensagem = str(entry.get())
    print(mensagem)

root = Tk()

entry = Entry(root, textvariable=mensagem).place(x=10, y=10)
button = Button(root, text='ENVIAR', command=enviar).place(x=30, y=40)

root.mainloop() 
    
asked by anonymous 07.02.2018 / 21:46

2 answers

0
def enviar():
    texto = mensagem.get()
    print(texto)

When you use a variable associated with an Entry, you should call the get and set for the variable. kinter ("message") as the name of a local variable inside the function, the first one was not visible to Python - you would have the error that you were trying to access the local variable before assigning it.

Just use the global name "message" so that it does not collide with the local variable.

    
08.02.2018 / 14:13
0
from tkinter import *

root = Tk()

mensagem = str

entry = Entry(root)
entry.pack()


def enviar():
    mensagem = str(entry.get())
    print(mensagem)


button = Button(root, text='ENVIAR', command=enviar)
button.pack()

root.mainloop()

Just add the .pack () and initialize the entry before function, so python can access this variable

link

    
08.02.2018 / 17:47