Dynamically change label value

Asked

Viewed 2,221 times

1

I’m a beginner in Python. How do I change the value of a label dynamically?

To be more specific, I have the following code:

#!/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()

Note the else: there, that’s where I’d like to insert my error message.

My idea was to leave an empty label there and only enter data when there is the error, but as I said I am very beginner and do not know any way to do this.

  • That’s what you want? error.set("novo texto") (fountain). Note: I have no experience with Tkinter, so I’m not sure I understand your question.

  • @mgibsonbr, unfortunately did not work out friend, it returns the following error: http://pastebin.com/KygvCB72 By the way, I made a mistake regarding the version of my Python, the installed here is 2.7

  • As I said, I don’t understand Tkinter, but his error suggests that the method pack does not return anything. Already tried to do in two lines? text = Entry(root, width=40) and underneath text.pack(). If this is correct, you need to do the same with the other components.

  • @mgibsonbr, I tried this, unsuccessfully also friend..

2 answers

1

Ola Wag could try as follows also:

import tkMessageBox

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
    tkMessageBox.showinfo("error", "erro a Url que você inseriu não existe")

recommend taking a look at how to treat errors would be a good.

http://turing.com.br/pydoc/2.7/tutorial/errors.html

1


Look at this other example:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from Tkinter import *

# Funções...

def Cumprimente():
    hello.set("Olá, mundo!")

# Interface...

gui = Tk()
gui.title("Olá Mundo")
gui.geometry("400x400")

btn = Button(gui, text="Cumprimente", command=Cumprimente)
btn.pack()

hello = StringVar()
lbl = Label(gui, textvariable=hello)
lbl.pack()

gui.mainloop()

Label text does not receive values directly from Python, so you need to use the "Stringvar object"...

hello = StringVar()

Then passing the text variable as a parameter of the Label object...

lbl = Label(gui, textvariable=hello)

In this way, when my "Greet" function is called, it is not the Label that changes, but the object "hello" (Stringvar) through the method "set"...

def Cumprimente():
    hello.set("Olá, mundo!")

Save and run the example to see the thing in action.

Browser other questions tagged

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