2

I would to like to know how to show a message inside Tkinter. I have this code but when i click the button, the message shows in Prompt.

from tkinter import *

janela = Tk()
janela.title("Poemas")
texto = Label(janela, text="O que você quer ler hoje?")
texto.grid(column=0, row=0, padx=50, pady=50)

def botao_Click(mensagem):
    print(mensagem)

#1° botão
botao = Button(janela, text="Buscar poemas",command=lambda: botao_Click("Nova msg"))
botao.grid(column=0, row=1, padx=10, pady=10)

#2° botão
botao = Button(janela, text="Buscar poetas",command=lambda: botao_Click("outra msg"))
botao.grid(column=1, row=1, padx=10, pady=10)


janela.mainloop()

enter image description here

1
  • 1
    Do you want to show it inside a new Label widget or update the contents of your texto widget? Commented Aug 6, 2021 at 22:25

1 Answer 1

1

Change the

  def botao_Click(mensagem):
    print(mensagem)

to def onClick(): tkinter.messagebox.showinfo("Title goes here","Message goes here")

And the button to

button = tkinter.Button(root,text = "Click Me", command = onClick)

The key is that the print method is to print to the console. tkinter.messagebox.showinfo("","") is for the popup message

Sign up to request clarification or add additional context in comments.

1 Comment

Why did you rename the function? Why did you assume that OP wants to show the message in a new window? Also OP wants to be able to reuse the function to display different texts based on the button that is clicked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.