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
@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
– waghcwb
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 underneathtext.pack()
. If this is correct, you need to do the same with the other components.– mgibsonbr
@mgibsonbr, I tried this, unsuccessfully also friend..
– waghcwb