Python 3.x Show typed data in an Entry on a Label only after pressing a button

Asked

Viewed 968 times

1

I wanted to know the following;
In my show, there’s:
Entry1 -> where the user enters some information
btn1 -> where it confirms the previously typed name and data is displayed on the console
lbl2 -> Where the data typed in Entry1 is displayed.

However, as the user enters Entry1, this information is already being displayed on lbl2. I wanted to display the information on lbl2 only when the user clicks on btn1, thus showing that the name has been confirmed.

Let’s go to the code:

from tkinter import *

# Componentes da Janela
janela = Tk()
janela.title("EmagreciPet")
janela.geometry('300x500')
janela.resizable(False, False)

# Variáveis
var = StringVar()
varChoose = StringVar()
# Funções Botão
def pegar_nome():
    content = var.get()
    print(content)
# Nome do Pet
lbl1 = Label(janela, text="Nome do seu pet: ")
lbl1.pack(anchor=NW)
# Digita o nome do Pet
Entry1 = Entry(janela, bd = 2, textvariable = var)
Entry1.pack(anchor=SW)
Entry1.get()
# Botão para confirmar nome do Pet
btn1 = Button(janela, text='Confirmar nome', command=pegar_nome)
btn1.place(x=135, y=17)
# Caixa de seleção Sexo Macho
rBtn1 = Radiobutton(janela, text='Macho', variable=varChoose, value=1)
rBtn1.pack(anchor = W)
rBtn1.select()
# Caixa de seleção Sexo Fêmea
rBtn2 = Radiobutton(janela, text='Femea', variable=varChoose, value=2)
rBtn2.pack(anchor = W)
# Nome do pet digitado na tela
lbl2 = Label(janela, text="", textvariable=var)
lbl2.pack(anchor=NW)


# Fim do programa
janela.mainloop()

2 answers

0

I got. Just apply on def:

def pegar_nome():
    content = var.get()
    print(content)
    lbl2.config(text=content)

lbl2.config(text=content)

and then change

lbl2 = Label(janela, text="")

0

Changing at least, what you need to do is change this part:

# Nome do pet digitado na tela
lbl2 = Label(janela, text="")
lbl2.pack(anchor=NW)

Note that if you remove the part of the textvariable, the text will appear only after the button is pressed. And some small changes in the function you set, just add something similar to this code after the print:

lbl2.config(text=content)

Browser other questions tagged

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