and thanks i am running this code. At no time does it generate an error, it just doesn't convert me from feet to meters. (Can you check and tell me where it's wrong?)
There would be 2 options to get the result, adding the numeric and pressing enter or with the calculate button. Neither of them work.
from tkinter import *
root =Tk()
root.title("Pies a metros")
def calcular(*args):
try:
value = float(pies.get())
m = int(0.3048 * value * 10000 +0.5)/10000
metros.set(m)
except ValueError:
metros.set("ERROR")
frame = Frame(root, pady=3, padx=12)
frame.grid(column=0, row=0)
pies = StringVar()
pies_input = Entry(frame, width=7, textvariable=pies)
pies_input.grid(column=1, row=0)
metros = StringVar()
Label(frame, textvariable=metros).grid(column=2, row=2)
Button(frame, text="Calcular", command=calcular).grid(column=2, row=2)
Label(frame, text="Pies").grid(column=0, row=0)
Label(frame, text="es igual a").grid(column=0, row=1)
Label(frame, text="metros").grid(column=2, row=1)
pies_input.focus()
root.bind("<Return>", calcular)
root.mainloop()
Your code should be like this:
I personally recommend that you do not use
textvariable
, it is not necessary. The main change I've made to your code has been to remove your variables and create theLabel resultado
one with an empty string.Once this is done, in your function
calcular
the text of the is configured byLabel resultado
assigning the value of your variable bym
executing aresultado.config(text=m)
, the same in your error :resultado.config(text="ERROR")
.Another personal note, I also consider that it is better to use
.place()
that.grid()
.What
.place()
we do is tell where our Label or Entry or etc... will be located using screen pixels.x = PixelesHorizontal
Yy = PixelesVertical
Center application in the middle of the screen when you open it:
I leave you this code in case it is useful to you one day.
window_width,window_height=710,555
--> You assign the dimensionsancho/alto
.The rest of the code is arithmetic operations to center the application to the center of the screen.
Also, in case you would like your app not to be resized, use
root.resizable(0,0)
There are many more things to know about Tkinter, any type of doubt or clarification write me in the comments. I will be happy to answer you.
All the best!