[Python] Mudar valor de uma label dinamicamente

Discussão em 'Python' iniciado por Stack, Abril 18, 2025.

  1. Stack

    Stack Membro Participativo

    Sou iniciante em Python. Como faço para mudar o valor de uma label dinamicamente?

    Para ser mais especifico, tenho o seguinte código:

    #!/usr/bin/env python
    from Tkinter import *
    import socket, webbrowser

    root = Tk()
    root.title("Test - waghcwb")

    def window(w=300, h=200):
    ws = root.winfo_screenwidth()
    hs = root.winfo_screenheight()
    x = (ws/2) - (w/2)
    y = (hs/2) - (h/2)
    root.geometry('%dx%d+%d+%d' % (w, h, x, y))
    window(270, 100)

    def Open():
    text_contents = text.get()
    url = str(socket.gethostbyname( text.get() ))

    if url != '0.0.0.0':
    webbrowser.open("http://%s" %url)
    else:
    #Erro para a label aqui
    print("Erro")

    info = Label(root, text="Testando", pady=20).pack()

    text = Entry(root, width=40).pack()

    button = Button(root, text="Enviar", command=Open, width=40).pack()

    error = Label(root, text="", pady=5)

    root.mainloop()


    Notem o else: ali, é ali que gostaria de inserir minha mensagem de erro.

    A minha ideia foi deixar uma label vazia ali e só inserir dados quando houver o erro, mas como falei sou bem iniciante e desconheço alguma maneira de fazer isso.

    Continue reading...

Compartilhe esta Página