Using . get() in your window with Tkinter

Asked

Viewed 28 times

-1

when I try to use the code:

import tkinter as tk

window = tk.Tk()

texto = tk.Text(window).pack()
tk.mainloop()
print(texto.get())

to return the value of what I wrote in the variable text the program returns me this error: Attributeerror: 'Nonetype' Object has no attribute 'get'

using Python console I managed to run it without problem, there is some way to make it run it without using the console?

  • .pack() returns None, vc is probably not using the exact lines in the terminal and script

1 answer

1

Well, from what I understand, you want to print the written answer on Tkinter’s window. I couldn’t understand if you want to print on the terminal or the window itself. I’ll do a demonstration for each of the possibilities, right? Come on.

Printing the answer on the terminal

from tkinter import * 
root = Tk()

#Criando o frame e estabelecendo configurações
arq = Frame(bg = "lightgrey")
arq["padx"] = 80
arq["pady"] = 5
arq.pack(fill='both', expand=True)

#Criando espaço para input no frame
inp = Entry(arq)
inp["width"] = 71
inp.configure(font = "Quicksand 12", bg = "white")
inp.pack(side=LEFT)

#Função para exibir o valor dado no terminal
def exibir():
   print(inp.get())

#Criando botão para imprimir
bot_visualizar = Button(root, text = "Imprimir", command = exibir)
bot_visualizar.pack()

root.mainloop()

Printing the answer in the window

from tkinter import * 
root = Tk()

#Criando o frame e estabelecendo configurações
arq = Frame(bg = "lightgrey")
arq["padx"] = 80
arq["pady"] = 5
arq.pack(fill='both', expand=True)

#Criando espaço para input no frame
inp = Entry(arq)
inp["width"] = 71
inp.configure(font = "Quicksand 12", bg = "white")
inp.pack(side=LEFT)

#Função para exibir o valor dado no terminal
def exibir():
   l = Label (arq, text= inp.get())
   l["padx"] = 20
   l["pady"] = 5
   l.configure(font = "Quicksand 12 bold", bg = "lightgrey")
   l.pack (side="left",fill="y")

#Criando botão para imprimir
bot_visualizar = Button(root, text = "Imprimir", command = exibir)
bot_visualizar.pack()

root.mainloop()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.