It is a graphical interface with which it is intended to go from the polar form to the geometric form of imaginary numbers.
I get the following error:
Traceback (most recent call last): File "main.py", line 33, in <module> boton1=Button(ventana,text="Calcular",command=geo_polar()).grid(row=1,column=2) File "main.py", line 22, in geo_polar solucion=print("Radio= "+p1+" y angulo= "+p2) TypeError: Can't convert 'float' object to str implicitly
This is the code:
from tkinter import *
import math
import time
ventana=Tk()
dato1=IntVar()
dato2=IntVar()
dato3=StringVar()
dato4=IntVar()
dato5=IntVar()
dato6=StringVar()
def geo_polar():
p1=math.sqrt(dato1.get()*dato1.get()+dato2.get()*dato2.get())
try:
p2=math.atan(dato2.get()/dato1.get())
pass
except Exception as e:
print("ERROR")
solucion=print("Radio= "+p1+" y angulo= "+p2)
entrada.set(solucion)
etiqueta1=Label(ventana,text="Pasar de geometrica a
polar").grid(row=0,column=0)
entrada1=Entry(ventana,textvariable=dato1).grid(row=1,column=0)
entrada2=Entry(ventana,textvariable=dato2).grid(row=1,column=1)
boton1=Button(ventana,text="Calcular",command=geo_polar()).grid(row=1,column=2)
entada3=Entry(ventana,textvariable=dato3).grid(row=1,column=3)
etiqueta2=Label(ventana,text="Pasar de polar a
geometrica").grid(row=2,column=0)
entrada4=Entry(ventana,textvariable=dato4).grid(row=3,column=0)
entada5=Entry(ventana,textvariable=dato5).grid(row=3,column=1)
boton2=Button(ventana,text="Calcular").grid(row=3,column=2)
entada6=Entry(ventana,textvariable=dato6).grid(row=3,column=3)
ventana.mainloop()
First, the error is because Python never does implicit type conversions. You are trying to concatenate objects
str
withfloat
directly:To do this you must do an explicit casting of
float
astr
:Now, concatenating strings is a very inefficient process since they are immutable, which means that when concatenating you have to create a new object
str
. It's simpler if you usestr.format
:or also string literals formatted in Python >= 3.6:
On the other hand, you have some more errors:
In the function variable
solucion
:First,
entrada
it doesn't exist in your code, in any case it must bedato3
.On the other hand, it
solucion
stores the return ofprint
, which isNone
. It must contain the string to assign to theStringVar
,print
in this case it doesn't make sense in the GUI:Another error occurs when passing the function to be called when the button is clicked:
this causes
command
the return ofgeo_polar
(None
in this case) to be passed since when using it()
you are calling it. You must pass the reference to the function, not its return:You also have some typos in the name of the variables
entada5
byentrada5
andentada6
byentrada6
.In addition to the above, some things that, although they are not errors, you should consider:
Importing via widcard(
*
) is generally a bad practice in Python and should only be used in cases where it is justified (and shortening the code to write is not one of them). Broadly speaking, it should not be used because readability is lost in the code by not explicitly specifying the namespace to which the identifier belongs, it populates the current namespace unnecessarily and, derived from the above, it can cause masking of identifiers between different namespaces, with consequent errors as a consequence.It doesn't make sense for you to do the following:
by doing this
entry
you are not referring to the instance oftkinter.Entry
created (or any other widget), but to the return of its methodgrid
, which is againNone
. This makes itentry
useless. Or do you directly:or if you later need to reference the widget, apply grid on a separate line:
The same applies to
pack
andplace
.Taking into account all of the above, the code can be:
It would be good for the readability of the code if you gave more descriptive names to the variables, so that whoever reads the code will find it more understandable at first. With this you can continue to complete the app.