This video uses the Kivy Python GUI package to create an application that can solve any problem based on Ohm's law in the field of electrical engineer/physics of electricity!
Kivy is a very versatile python graphical user interface package because it is inherently cross-platform compatible to run on mac, windows, Linux, iOS and android! By knowing some basic syntax of the Kivy design language and some common python object oriented programming concepts you can create this very intelligent and easy to use application to solve for voltage, current, resistance or power!
Timestamps: 00:00 Intro and Background 03:05 Setting Up Basic Kivy App 10:00 Make a Grid Layout and Add Label Widgets 14:50 Creating Box Layouts inside a Grid Layout 24:40 Adding Button Widgets 32:00 Bind Kivy Buttons to Functions with Passed In Parameters 38:00 Solve Ohm's Law Using Python Programming 52:28 Using Size Hints to Resize Kivy Widgets 54:50 Outro
what's up you guys welcome back to the
channel in today's video i'm going to
show you how to create this ohm's law
electricity uh solver in python using
the kivi graphical user interface we're
going to do it totally from scratch
[Music]
so in the world of physics there are
these four problems of these four
variables power voltage uh current and
resistance and they all really feed off
of each other
ohm's law is actually the formula at a
base level that says v equals ir or
voltage is current times resistance but
since power is voltage times current or
resistance times current squared or
voltage divi squared divided by
resistance
it's pretty natural to group power in
with these three so technically ohm's
law is v equals ir
but power is a really normal extension
off of that and you can see from this
wheel i have on screen right now you can
solve for any variable if you know the
other two variables and so what i
thought would be a really neat problem a
really neat python project would be
create an application like you can see
on the screen where you give it any two
problem any two of these variables and
it can just solve for you for the other
two and we can use this system of basic
uh equations and just create an app that
can tell
what givens it has been given and then
it'll even show you the formula that it
uses to come up with the solution
because obviously as some people pointed
out on my kinematics
video that i made using tkinter i'll
leave a card to it somewhere up here
but the goal is not to decrease the
learning of anyone who would need to use
this tool so you would still want to see
how you got to that point so you could
always check and verify your math and
make sure that like if this was
something you were using in school
you could still pencil down like what
formula got you there it's just kind of
a shortcut and it's a useful programming
exercise so
hopefully you find this useful
hopefully you think this is kind of a
fun neat project that you'd like to
follow along with if you do want to see
something specific in the future be sure
to leave comments on the video
all the people who leave constructive
feedback i really really appreciate
those guys um and gals uh special shout
out to a couple accounts that have been
leaving like tons of great feedback
recently um dale
dubper
empire of programmers and uh flio inc
i'm sorry if i mispronounced any of
those names but these uh people comment
on a ton of my videos uh really really
great support and so if you want to see
something in particular on a future
video be sure to be like them subscribe
to the channel leave some likes leave
some comments and if you really want to
support the channel check out the link
to my patreon in the comments below uh
in the description of this video and
without any further ado let's get into
the tutorial
all right let's get right into this
if you have never used a program with
kivy before i recommend you check out my
kivi intro series playlist i'll leave a
card to that as well somewhere in the
top
around now
because i'm not going to go super in
depth about every kivi concept in the
video i'm really going to use them and
i'll talk about them as i use them but
if you want to know some background of
why we're doing things certain ways
feel free to check out that playlist i
go much more rudimentary um there so for
now let's just get started building this
app and i'll probably click back to this
um wheel fairly often because these are
going to give us all the formulas that
we use in our decision making but for
setting up the app we really don't need
that yet
to set up the app kind of in kivy the
way you're always going to want to do
this is uh start by doing some imports
if you know generally what your app's
going to look like so you actually don't
have to do this like import kivi that
you have to do with almost any module
because the way we're going to do it is
from kivy dot
and then we'll do things like app and
then dot user interface things like that
so you don't need to import the whole
kivi module with an import statement
we're going to do it this way
um
so for to start we're going to import
just
from kivvy.app import app and then as we
build this app out we're going to be
importing more things like buttons
labels
layouts text inputs things like that but
for now we're just going to start with
the app
and we're also going to have some other
input statements like we'll need math
and function tools before this is over
um
but okay so to get started we're just
going to set up a class for our app and
we're going to call it ohm's law
app just like that and then in
parentheses you tell it what it is and
it's an app so just like that
um and then we're going to give it this
line define and then build self and in
general like i said um this is just how
you want to have
the uh like the app set up this is
pretty generic kivi
you know once you get more familiar with
the program there's definitely different
ways you can go about this but for now
since this is kind of more of a beginner
tutorial just follow along and if you
have any specific questions let me know
in the comments below but then um
so inside build tell it to return
whatever like starting screen you want
to have since we're going to have a
single screen application here we're
gonna call it the ohm's law input screen
um
but if like you wanted a startup menu
this would be a good place to put the
menu and then from the menu it would
call the other screens
um
but so we're gonna name our main screen
that the whole thing happens on the
ohm's law input screen and we need to
come up above and just kind of make
another class for that so this class
ohm's law input screen
is going to be a new thing we'll have to
import because i want it to be a grid
layout
and again i don't want to talk too much
about all the different layout options
box layout grid layout
float layout anchor layout there are a
lot of different ki layouts i don't
really want to go into all of the
options in this one so just so you know
a grid layout in general separates your
screen up into rows and columns and then
as you add widgets it's going to put
each widget in the next available space
what i'm going to do is i'm actually
going to have an overarching grid layout
and then for some of the widgets in that
layout i'm going to make them additional
box layouts so
there's so many different ways you can
design in kivy this is not like a
full-blown course on kivy this is how to
make a specific app so let's keep going
but you'll see we need to import this so
if you're using a good ide like i use
the pycharm one from jetbrains
when it recognizes there's something
that hasn't been
imported it usually can suggest the
import statement for you and just tell
you but if you don't have an id that can
do that just go up and do from
kivy.uix.gridlayout with lowercase like
that
import and then a grid layout with a
capital g and an l that's called uh i
believe camel case or maybe pascal case
there's all these different ways
of naming it but so first letter caps
for grid layout
and now we can define this uh grid
layout in here
and when you're defining a layout you
want to start kind of like a class in
python um even if you're not doing like
a kivi you want to start with this def
in it and then
you pass in self and this star star
quarks which is i believe keyword
arguments but it's essentially saying
like pass in
any parameters that i give it
and then we're going to call super which
is saying inherit the
any properties of a super class that i'm
underneath and for this it's just the
name of the screen so ohm's law input
screen like that and then comma self
like that and then do dot underscore
underscore it underscore underscore star
star quarks okay so i know that if
you're not familiar with kivy and you're
not like a super experienced programmer
there's probably a lot in these lines
that don't make a ton of intuitive sense
but just follow along for now and as you
become like a better programmer
you'll start picking up what all of
these things mean so
for this overarching layout i'm actually
going to make it just a one column
grid so it's not exactly a grid
you can think of it more as like one
vertical
uh line of widgets but you'll see why i
do that i think as we go through the app
and for self.rows i'm going to start
with 10 we might actually need to
increase that as we go but let's just
start there
and now i want to come down back where
we were building like the code down here
below and i want to just add the last
piece to sort of make this thing go so
it'll be if underscore underscore name
underscore underscore
equals equals and then in quotations
underscore underscore
main underscore underscore
and then ohms law app dot
with parentheses dot run with
parentheses so
this is saying if the screen name is
main which by default your first screen
that pops up is going to be the main
run the app
so
let me see this might not actually run
but
yeah there we go so we haven't put any
widgets on the screen so it's obviously
an all black screen but you could see
just with what we have now we got enough
to make the screen pop up and now we're
going to start actually putting things
on the screen so let's um start adding
like the labels and the inputs and the
text
and all the things we want to prompt the
user with
okay so the first kind of widget we're
going to use is going to be a label so
let's start with a title label and it's
going to be title equals and then label
with a capital l and then we're going to
give it some
some information inside of here like
what kind of text it needs to have but
you can see we need to import the name
and we want this to be kivi.uix.label
so it's going to give you just like the
grid layout kv.uix.label
import label with a capital l and now we
are defining a widget so we can do um a
few things we can start by saying what
text we want the label to have and i
want this to say ohm's law solver
now the
apostrophe is a special character inside
of strings so if you want an apostrophe
to appear you need to do this backslash
apostrophe like that
and that will give us ohm's law
solver okay and so that's the title
we're gonna give it now there's um
something else you can do uh with labels
which is nice and buttons um you do this
font underscore size and then you give
it a size and i'm going to do 32 sp
um
and so that's just going to make it a
bigger font
you can play around with style all you
want additionally you can do color which
if you're familiar with rgb colors this
is a little different this is like the
rgb values divided by 255
so i'm gonna do like a gold that i liked
during playing around here but
just understand this one is actually
saying 255 divided by 255 for rgb
and so this is like 0.8 times 255
divided by 255. so basically we are
calculating the decimal equivalence of
rgbs out of two five five um it's just
another form of like playing around
style all you have to do if you have an
rgb color that you like is divide them
by two five five put them in here on a
scale of zero to one and that's how you
get your color for the label
specifically color changes the text
color not the background
and then another argument we're gonna
give it right now is just bold equals
true so this makes the text a little bit
easier to read
and now what's cool is we've defined the
label right here just just with this
information but there's a second piece
we have to do self.add widget and then
just give it the name of the widget so
for us it's going to be title and now if
i boot this thing up
you'll see we get ohm's law solver it
takes the whole screen up because we
haven't put anything else on the screen
but you can see it's bold it's big text
and it's gold so that looks pretty cool
and we just played around with a lot of
parameters on a label widget and that's
actually going to make a lot of our
widgets that we're adding here in the
future a little bit easier so
let's go ahead and add a second one that
we'll call
in
and i didn't spell it right there we go
instructions
and let's make the text on this one
enter
known values and tell them that there's
a minimum of two so the thing to solve
this um
to solve these ohm's law problems we
need a minimum of two knowns right
so uh let's go ahead and say font size
for this one doesn't need to be as big
but we'll still want it to be kind of
larger than the default which i think is
12. so we'll make this 20.
i'm not going to give it a special
color it's going to default to white on
a black background which will look fine
for instructions
and i'll leave it bold i'm pretty much
all text i make bold unless it looks
weird for some reason because it makes
it easier to read and then let's put
that on the screen let's do a quick uh
style check see how it looks
enter known values minimum two that
looks pretty good so let's keep going
now the next thing obviously we just
asked them to enter their known values
so the next thing is we want
voltage current resistance and power
all to be a label and then a button
and i'm going to show you here why we
are going to want to do this as a new
class
because with the grid layout
right now it's just automatically
dropping them in the next available spot
so what i want is the next available
spot underneath instructions to be a
label on the left side and
a text input on the right side so we're
going to want to make a new class that's
called
[Music]
label and input and let's go ahead and
do that and you're going to see why this
saves us a lot of writing time
once we define this thing
so oops there we go
so let's go ahead and say class
label
and
label and input and i'm going to use
that kind of uh like first letter of
every word caps standard that kibby has
because it's already here
and i'm going to make this a box layout
okay
and we have to import that as always
and let's go ahead and say the same
thing because we have a new class here
def init
self and then we're going to pass some
things in we're going to pass the text
that the label box should have so input
text and you can set them up here in the
beginning with like an initial type or
value so we'll just say none
and then input
color we'll say
input underscore color because i want
the um different entries to all be
different colors so uh by default we'll
make them white and then we'll pass in
better colors as we go so
we're going to pass in uh quarks as well
you have to do that um
so just take my word for it and then
we're doing that same super label and
input
and self
and then dot underscore underscore init
underscore underscore star star quarks
keyword arguments so i will say kivi of
like pygame kinter and kivi which are
the three gui packages i use the most
with python i think kivi might be the
trickiest because it has its own design
language the kivi design language you
even can design the style in a separate
app that's or a separate um
file that's a dot kv extension and it's
totally written in the kivi design
language
there's just a lot of syntax that you
have to get familiar with if you're
going to make a program in kivy whereas
i think pi pygame and kinter kind of
lean on the python inherit like language
and style a little bit more um but so
that's just my side
note about uh
about kivy hears i do think it's
trickier than kinter and pygame but the
real value in it is it's
it's cross
compatible with everything so mac
windows linux ios android you can build
a kivy app that runs on all of those
natively so if you're gonna make in a
mobile application in python qivi is
like your best option for it um there
are some other ones but in general i've
found qivi is the most you know
immediately useful for creating mobile
apps
so we're going to make again we're going
to make some rows and columns defined in
this box layout and the real reason i'm
doing one as a grid layout and one is a
box layout is i just want to show you
two different layouts but these two are
pretty interchangeable
especially for the purposes we are doing
here
but for these i'm actually going to say
two columns and one row for each of
these that we define
and i'm gonna say first we want to make
a label that i'll say
self dot
label
now label's stupid i don't know let's
call it value is going to be equal to
a label where we say text
equals and then since this is a class
we're going to have a bunch of these so
we don't want to make this too specific
we're not going to pass in
the same text for every
label so we're going to have this read
from the variable that we pass in
and then
let's say color is going to be equal to
input color so the other variable we
pass in
let's make bold equals true like that
and then we got to do this self.add
widget and then self.value just like
that
value might not be the most logical name
but i already did it so let's move on
self.input is equal to a text input so
this is a new type of widget uh make
sure to import it again from
kivy.uix.com input import text input
and this one's pretty easy uh it's it's
by default gonna pretty much look how
you want to look so we'll say multi-line
is equal to false which just means don't
let the text break onto a second line
keep it to one line this is supposed to
be you're entering numbers so it's not
like you need multiple lines anyways
but yeah that should be all we have to
do to define this now we just want to do
self.add widget for self.input
and what's cool is this little class we
just made here we're about to add four
label and input combos to our
overarching
to our master uh ohm's law input screen
and so even though we didn't just add
anything to the screen it's about to
become super easy to make for these
things so let's do voltage first so
self.voltage equals
our label and input class and now we
just give it two uh two variable
arguments so if you remember we said
first was going to be text so that'll be
voltage and then i'll tell it it's v
and the units are volts like that
and then colon space
uh apostrophe and then let's give it a
color um i'm a little too lazy to pick
out like super stylish colors so i'll
just make it red
but now the second piece all we have to
do is self.add widget for self.voltage
and what we should get is a label and a
text input
that tells us about voltage so let's see
okay voltage v volts and then a spot
where i can enter you know the voltage
and obviously there's enough space here
that i could enter a very large voltage
but typically it's going to be like zero
to you know 480 at a maximum so that
looks pretty good we're going to play
around the style at the end as well try
to make things um spaced a little more
logically but for now let's just go
ahead and add current resistance and
power as well so
i'm actually going to define all four of
them and then
add all four to the screen so let's see
if i can do that without making a
mistake
so we'll do v equals ir that kind of uh
that kind of order we'll do self.
voltage current resistance and then
power
and this is going to be
current and then it's going to be this
one is i
and the units on current are amps
and then this one is
resistance and r and the units on that
are ohms so this is some pretty basic
electrical engineering uh sort of stuff
but i do think that anyone interested in
science computer science um
you know especially programming
where the fields are not that far apart
like knowing some electrical concepts
and knowing um you know some computer
science concepts they go together really
well so
uh it's not a bad idea to be familiar
with ohm's law really so even if you're
thinking well i like the python element
of this more than the electrical element
of it um
you know i think open your mind this is
cool knowing both
[Music]
so we're going to do red and then we're
going to do
green and then this is not exactly a
bright blue it's a little bit easier to
read and then for power we're gonna do
uh r and b which gives us kind of like a
pink again if you don't like any of my
colors that i use play around with the
style on your own these are just rgbs so
pick whatever you want
and
i just kind of try to use some easy
numbers some easy colors because i want
this to be more of a tutorial on the
substance of this app and then obviously
if you decide to make this your own then
you should play around this style
because you want it to look
pleasing to you and obviously to users
if you plan on sending it out so then
just do self add widget for all of them
and i believe
that should give us
yeah these nice four
rows um that each of them is going to
say uh it is now associated with a
different variable so later when we
actually do the functionality of solving
for the different things we can just
address
like the voltage text input box and get
the value out of that so
that's really the power of creating this
label and input class
is now we just made eight widgets just
by defining them all we had to give them
was text and a color and it put a label
and a button on or a label and a text
input on for everything so that's super
duper useful
and then i think what we want to have
next
is a
series of buttons that we want to say
like solve for
voltage solve for current solve for
resistance solve for power because
obviously you can solve for any of them
and so uh maybe you've guessed it but i
want this to be four buttons all in the
same row and since our overall grid
layout is only one row wide we're going
to create a new class for our buttons um
so to do that i'm going to call it class
button box
and i'm gonna make this another box
layout we don't need to throw a third
layout in the mix i'm not gonna
show you guys like an anchor layout or
anything
um so we'll do def in it in it and then
self and then quark should be all we
need for this one because uh we're only
going to make one
series of buttons but this would be
useful like if you were going to try to
combine this and the kinematics
project that we already did in
you might want to use this same button
box layout over there so maybe you would
want to pass more variables in in that
case
but we're going to keep it simple for
now so
super button box
comma self and of course by now
hopefully you're just used to seeing
these things um
or you're getting there
but uh star star quarks and for this one
it's going to be self.calls we're going
to make it four columns wide but then
only one row so self dot rows equals one
and then uh i'm going to make four
buttons real quick so i'm going to say
self dot uh solve for v is equal to
button with capital b
uh the text equal to
text equal to solve
v so solve for voltage
and then uh let's make the colors the
same as what we just defined them in the
label so one zero zero
and let's make it bold bold equals true
and let's take this same
oh this is our first button cool so we
have to uh import this i was like that's
weird it's not working so we need kivy
dot uix dot button with a lowercase b
and then import button capital b
okay i was like that's weird we've
definitely used buttons but no we
haven't
all right so we're gonna do this four
times again so v
i
r p
solve for v
solve for i
solve for r solve for p just like that
and then let's update the colors we had
r then we had g
and then we had this kind of easy to
read blue which is a
all the b one b with uh half g
um
and
p is this kind of like pinkish color
okay but then we're gonna do self.add
widget and we're gonna add all of these
so solve oh self
dot solve v
and i'm going to copy that four times
and so basically what you're seeing
hopefully is that a complicated app
layout is really just up you want to
break it down into simple
um sub classes so like if you run this
now so we are going to go up into our
master screen and just do self.add
widget widget
and we're gonna add a button box so self
dot buttons
is gonna be equal to
button box just like this
and then self
dot add widget
buttons oops self dot buttons
self dot buttons even after you've made
a lot of programs you'll uh you'll
forget things silly little syntax things
so what you should see now
is uh okay so we've got these four
buttons going across the bottom of the
screen
to solve the four variables and they
don't do anything right now because we
haven't written the code for them to do
anything
but actually if you're looking at this
app you have to remember that the
overarching like
grid layout is only one column wide so
each row is considered one widget to the
overall app
but you have the ability to take another
layout and stick it inside of one widget
so these four buttons are technically
one widget and that's the buttons box
but inside of buttons box we put four
widgets in there we put four separate
buttons and you could even do
like uh all eight of these all four of
these labels and all four of these text
inputs
inside one widget and so then you could
do like a two row two column layout or a
four
row two column layout all within one
widget and so you make these complicated
apps that really just break down to a
series of more simple applications it's
almost like if you're familiar with like
advanced calculus there's this thing a
laplace transform you take a very
complicated math problem solve a
comparable
simpler math problem and it translates
into something that actually uh gets you
pretty close on the complicated version
so i like to think of this almost as
like a laplace transform of complicated
apps
if you thought that was stupid i don't
care leave me alone
um okay now let's just put the last two
labels that we want to show on the
screen and then we'll start doing the
button functionality because that's the
actual programming part of this the rest
of it is just kind of learning kivi
okay but so then what we want to do is
uh we want to make a couple labels so
we'll say self.answer is going to be
equal to a label that just has the text
of
answer in the beginning and we're going
to update what that text says when we
actually solve the problem but just to
load it in initially we'll have
self.answer
and then we're going to show the formula
so if you did watch that kinematics one
you know i think it's useful to show the
actual work because uh we want to like
say you were using this to shortcut your
homework um you're still going to be
required to show your work more more
likely than not and it's useful at any
time to be able to see how you're
getting an answer purely just to verify
that you're getting the right answer so
um
we're going to show the formula that we
use to get the answer and that'll be
kind of an easy
addition to the pro project but so then
let's add both these widgets self.answer
and self dot
add widget
self dot formula
okay
so let's load this up for like a final
style check
all right i think this is going to look
pretty good now um so i think we'll put
in our knowns a minimum of two and then
we're going to write the
programming such that when you click
these four buttons they're going to go
to the solve function that we're about
to make take the known values
and uh try to produce the uh the correct
solution we'll then put the solution
here and we'll put the work that we use
to get it here so another nice thing
about
kivi in general is it is natively
resizable so that again helps with the
cross compatibility of it you can change
uh style a little bit and we probably
will
but by default every widget is going to
have the same height and then every
widget is going to have the same width
by default so like we made these four
buttons and we didn't change any of
their default sizing and so they all
have one quarter of the width the same
with this label and this text box um
we will talk about size hints after we
make the functionality because i think
you've come a half hour into a tutorial
let's actually get into making these
buttons do something
so now when you want to use a formula
for kivy where you pass in parameters
you need to have a function tool that's
called partial
and this is kind of comparable to like
if you're familiar with the kinter
using like lambda when you push a button
we're going to do the same kind of thing
so we'll come down below where we throw
the buttons on so here we have
self.buttons equals buttons box
and we want to make it to where
self.buttons
dot
solve v so let's start there
um and then to bind the press of a
button to a command you do this self.v
um or
self.buttons.solvv.bind so the name of
your button and then dot bind and then
you have a few options you have like on
press on release uh a few others you can
look up the kivi um text online
but we're gonna create a formula that's
like self.solve but then we need to pass
in what we're solving for and you can't
do it the normal way where you just put
these parentheses here and then you say
what you're solving for that doesn't
work
with kivi's style so what you have to do
is you have to do
on press equals and then this partial
and partial there you go and we do
self.solve
and then comma and then the parameter
you want pass in and then close
parentheses and so to uh use this
partial you need to import from a module
called func tools.partial so function
tools
import partial just like that
and now what's nice is we can create
this formula self.solve for all four of
our buttons v i and r
uh and p and all we have to do is just
pass in what we're solving for so then
we'll do all the rest of the work inside
of the self dot solve function so you
know i think that's pretty uh useful
right there you just have to know this
little bit about partial i think there's
a comparable way to use lambda as well
but we're going to go with partial
because that's how i learned it and i
think that's pretty easy to be honest so
all right so underneath where we're
adding all these widgets whoops
but still inside of the ohm's law input
screen
we want to come down and we want to add
another function so define and we called
it solve so self is always going to get
passed in
we'll call target like what we're
solving for is the target and then when
you're doing something that is a button
like formula so you're solving for a
button you want to put just button in at
the end um it's going to throw an error
and say you didn't pass in the right
number of arguments if you don't have
this button here
so just
i guess take my word for it on that one
if you haven't used it or you can try
deleting button out and see what happens
so we're going to start with this values
list that i'm going to have as all zeros
and then we're going to start solving
for the different variables so
first i'm going to say
if self.voltage.input.txt
is not equal to and then empty string
okay so this is saying if you entered
something in voltage colon
then we're going to say values 0. so
voltage is going to be value 0
is going to be equal to and i'm going to
convert all these strings into floats
now so i'm assuming you entered numbers
because you want to solve for a
numerical thing
is
self.voltage.input.text so this is how
you get the text that was entered inside
of that text input
if you remember inside the text input
like the label and input we called that
whole thing self dot voltage
so we go to self.voltage and then we
look at the label and input and it's the
text input which we called input so what
we're saying is self.voltage which is a
label and a text input and then dot
input which specifically was the text
input and then dot text which is
whatever we typed into it
so we're checking to see that something
was entered in there we're going to
convert it to a uh
we're going to convert it to
um
a
what am i trying to say we're going to
convert it to a float in here so that we
can do math with it
and we're going to do that same thing
four times so do a little copy paste and
try to go through quickly here again i
know i'm trying to cover a lot and i'm
trying to not make the video too too
long
so if you have questions about anything
specific the best way to get them
answered is just ask them in the
comments a lot of the people who watch
these videos are smarter than me and so
someone other than me might even get you
an answer before i do but i do try to
check those fairly often so
self.current.input.txt
self.resistance.input.txt
self.power.input.txt
okay so we're going to get those four
and this is just basically updating this
initial list of zeros um that we made in
the very beginning
and then i'm just going to do this next
step you don't need to but it's going to
make the rest of all the formula
checking a lot easier i think i'm going
to move all of the values we just
calculated into these little variables
that i'm just using in this solve
function so that uh it i can create some
very simple formulas using vi rnp which
is kind of what i'm used to
okay
so now we're gonna start actually using
all of these formulas all of the v i r
and p formulas based on what our target
is and let's start with voltage so i'm
gonna say if target equals v which is
saying um you clicked the button to
solve for voltage
well first thing and probably the
easiest thing is if v is now not equal
to zero um then we're just going to
so we're going to do two things for
every scenario we're going to say what
formula we used and put that in like the
formula work variable
um
and then we're also going to say what v
should equal and the thing is if v does
not equal zero and you wanted to solve
for voltage that means you entered
something in the voltage box and then
clicked solve for v so we're going to
assume that
you accidentally clicked this and just
for work we're gonna say v is equal to v
and then uh we don't have to set v equal
to anything new because um i guess
doesn't like that style
because you've already solved for it but
now the next scenario let's start
looking at other combinations of
variables so we'll say
all right so you didn't enter something
in v so that's what the l if means it
means else if so v does equal zero but i
doesn't equal zero
and then there's two scenarios here
there's uh you
you're trying to solve for v
and you know i well now we need to know
whether you know r or p so we'll check
uh r first and we'll say
if r does not equal
zero so now we're saying you've entered
i and r we want to solve for v this is
great we say v equals i times r
and then the work is really easy so the
uh
i times r like that
and then we set this work variable
that's just a string showing what
formula we used
equal to
i times r so in general you're gonna see
this you're just gonna see me saying the
actual math that we're doing
to solve for v
and then you're going to see
the exact same formula used just for the
work
all right
now
we're saying l if i does not equal 0 and
r does not equal 0. well the contrary to
that would be you know i and p instead
of i and r
so
if p does not equal 0 now we're saying
you know i and p and you want to solve
for v so you can come back to this
circle if you want we're solving for v
we know i and p so it's just p divided
by i
so v is equal to
i divided by p
and the work for that one
is hopefully you're picking this up
v equals i divided by p
and i know some people said on the
kinematics project that like my math got
a little confusing but really it's just
checking what variables were entered to
make simple decisions about which simple
formula to use so if this will make
sense to you then the programming should
make sense as well i am trying to
explain my thought process but it
there's kind of no super easy way to get
all these formulas written down um
without going a little bit quickly
because it's it would be a really long
video if i just showed
20 different formulas and how i got
there
so now the last scenario where we
actually got valid inputs which means at
least two knowns would be we have p and
r
and so in that case v is going to equal
math dot square root and so this is
something new we have to import because
uh we weren't using the math module yet
so make sure you import math just so
that we can use this square root because
if we know p and r and we're solving for
v
it's the square root of p times r
so
you just do p times r and then for the
work for this one since it's not super
easy to type the symbol for square root
i'm just going to say the work is equal
to and then just sqrt like that and hope
that they know that mean square root
um
and then p times r so like that and now
the last we do need one more else
because uh else would be you didn't
enter two
valid inputs so like if you just entered
i or you just entered p
um we still want to say like invalid
inputs like that
okay
and then we're going to make this new
variable so that there's actually some
text and we will say answer is equal to
a string
and i'm making a format string so we can
use v and i'm going to say voltage is
and then colon and then curly brackets
string conversion now of v because v is
a numerical value
and i guess actually it doesn't need to
be a string if we do formatted uh silly
but we can round it because if you
divide to like 12 decimal places
that's not really useful in general i'd
say like three or four is plenty
you can play around with that if you
want more or less
but whoops and then i meant for volts to
be outside of the curly brackets
so like this so basically um we're going
to come down after we check for the four
targets and we're going to say at the
very bottom outside of all of our if
statements but still inside the solve
function we're going to set two things
we're going to set self.answer.txt
equal to
the answer which is this string voltage
is you know volts and then
self.formula
is going to be equal to the work
variable that we come up with
so now
this would work and it should work if we
solve for voltage so let's go ahead and
just test that to see kind of a sanity
check here let's say we have two amps
and two ohms and if you know v equals ir
if i click v it should tell us that
there's four volts and the formula
should say v equals ir
which is sweet it works that's great but
this is only going to work for volts so
now i'll try putting 2 in power instead
and solve for v and we get v equals i
over p which is cool and now let's see
there you go so if we have uh v equals
square root of p times r it still solves
for v so this is awesome we're one
fourth of the way there if you want if
you see where this is going you can just
take this wheel and apply the same kind
of thing to the solve functionality if
you don't like following along with me
but if you're enjoying it
let's go ahead and do uh i r and p
together here so we just did v
now we're gonna say l if
target is equal to i and since uh
hopefully you understand where that
whole first series came from we're going
to go pretty quick
making this apply to the other ones so
now it's going to say if i does not
equal 0 that means you entered something
for i and so now i'm going to just swap
kind of i with v here i think
and so we'll say if you know v and r and
you're solving for i i equals v divided
by r and then the work is just obviously
going to say i
equals v divided by r
then the next one will be you know v and
p now and so that means i is equal to p
divided by v
and the work is just going to be i
equals p divided by v
if you don't know where any the formulas
are coming from take a screenshot of
this look up the ohm's law power wheel
on google images it'll show you this
exact image
this is where all the formulas are
coming from and i don't want to click
back to it every single
iteration of it because this would be a
very very long video so now let's say
you know p and r but you don't know i so
you're solving for i
that formula is i equals
the square root so again we need the
math.sqrt and then this time it's just
the square root of p
divided by r so like that and so the
work for that is going to be i equals
the square root of p
and then divided by r
alrighty so let's go ahead and let it
auto reformat so now that should work
for everything for i i'm gonna go ahead
and copy the v
and i stuff
um
and we are gonna do do do oh crud did i
not copy the else
statement oh pete silly goose
there we go else invalid inputs and then
answer
instead here should be current is round
am
okay and so i think i goofed that up
down here as well so we're gonna just
need to remember
else i is equal to invalid inputs
yeah all right we're okay this won't
this will be an easy fix but if target
equals r now if r does not equal zero
then work is gonna be r dot r at the end
i wanna remember to change this to
resistance and we'll go back and fix the
formulas in just a second
the units on resistance are ohms and
then this one will be target p so if p
does not equal zero we just say p is
equal to p what's wrong with you you
should know that
and then we'll copy this last little
block here this else statement that i
cut
and else p is equal to invalid
uh
inputs the answer will always be
power is
p
watts
all right now we can fix the formulas
for r and p so let's say uh to start in
r let's say you don't have you're you're
given i and v as the first scenario so i
and v
that means that r
you think back to v equals i r that
means r is equal to v divided by i
make the work r equals v divided by i
and then otherwise you know i and p well
that means that r
is equal to p
divided by
i squared and to raise something to a
power in python it's two stars if you
don't know that
but r equals p divided by i think the
little carrot for the work will actually
make it clearer because this is a little
more universal the sign for raising
something to a power
all right
so that's work equals p
divided by i squared and then the next
scenario is going to be for solving for
r that we know p and v that's the only
one we haven't done so far
and this means that r is going to be
equal to
v squared divided by p
and make the work to do r equals v carat
2 divided by p just like that and then
otherwise it means we haven't gotten two
inputs and we weren't given r directly
so we will keep that and now we are on
to the last bit of
formula solving so we'll say target
equals p
um obviously p we already did that bit
now let's say we're given v and r uh the
formula for p is is v squared
uh divided by r so then the work for
that becomes p
equals v
carat two because i decided that looks
better if you don't like it
do whatever you want
okay and then the next we'll say v uh
and then i
and so if we have v and i and we're
solving for p
that formula is just p equals v times i
which is pretty easy if you know
like
volts times amps is
watts
if you really dive down into your units
that's kind of the same
thing so hopefully that one makes sense
to you
now uh l if the last scenario here is
going to be r and i so l if i and r and
we're solving for p
and the formula there is p equals i
squared
times
r okay so
p e
equals
i squared times
r just like that and now this formula
which i know it kind of looks like we
did the same thing four times but there
really wouldn't be a good way to put
that in a function or like a class
because the formulas are all slightly
different
so
i think this is really actually a pretty
intuitive way to do it i'm gonna change
that to an l if
and uh
why does not like it l if target
oh yeah this whole thing is one
indentation too far in isn't it
how's that
l if
if
l if trying to figure out why it's
unhappy l if
and is it you you're too far in as well
like that
yeah that should work
um
so i think to test this the best way
would be go online and look up some ohms
law
practice problems
but i'm going to just open it up with
you real quick test a few make sure
every button is working so let's put 2
and 2 in there solve for v so v equals i
r
p equals i squared times r that looks
pretty groovy
i'm going to put 6 in here solve for v
square root of p times r
um square root of p divided by r so you
can see it's giving us each answer it's
giving us the formula of how we got
there and if we were to click on one
that we
put in as a known it's going to give us
just like hey you told us that already
so uh you know i think this looks really
good
i definitely recommend if you're making
it on your own look up some practice
problems compare your answers versus the
ones online
i don't want this video to get too too
long and we do still want to talk about
style because i know i mentioned um
discussing size hints a little bit
earlier so let's get into that now
so i'm going to come down and start in
my uh label and input
box layout because this is a pretty easy
place to talk about it so we have these
labels and these text inputs and i
actually think the labels don't need to
be half the size of the screen so like
by default
you know they're going to get half of
the width available to them well we can
step that down a little bit by saying
0.4 and then one is saying take the full
y available to you which is fine
um
and then we want to take this size hint
because all it's saying is uh i want you
to still be resizable but i want you to
kind of change what percentage of your
available space you're using
and so we'll say 0.4 and then for the
actual input box we'll say 0.6 and let's
see what doing just that does
so now hopefully you can see this line
was originally down the middle kind of
between where these buttons were and now
it's shifted over here because we've
said well we only want the label to take
up 40 percent of the screen so that's a
pretty easy way to resize it
one more thing i think we should do
because what we did there is we resized
some x parameters i think we should take
the text like the labels and i think we
should make them all only half the
height available because obviously the
buttons and the um like text input are a
little bit more important than just our
regular text so i'll come into the
main class here and all the places where
we have a label
um so like the ohm's law solver i'll say
well use your full x span available i
want you to span the whole screen but i
only want to be half as tall as like the
buttons and text input stuff so realize
what you're doing with size hints is
you're resizing stuff relative to other
stuff on the screen this is part of why
kivi's like automatically resizable
defaults are so useful it'll always fill
the screen you're just giving some size
hints so that stuff you want to be a
little bigger and a little smaller
resize so there i made all four of the
labels take up half of the
vertical height that
that the like buttons in main input
areas take up so hopefully that looks
pretty good to you
i still think it's super easy to use
it's very readable
you know you could make the font sizes
bigger
you i think hopefully in this video and
if you're familiar with kivy in general
you know how to take the style and make
it your own i hope you've kind of
enjoyed seeing the functionality that we
put into this
in general i think cross-disciplinary
apps are really where like
programming engineers who also have a
fundamental understanding of other
engineering concepts
can really find a lot of value because
you're creating easy to use programs
that other people will actually be able
to use for real concepts engineering
concepts so
hope you found this video
useful and enjoyable and as always if
you have any questions comments or want
to see something specific in the future
let me know about in the comments below
i really appreciate the feedback if you
want to become a super supporter of the
channel i really appreciate anyone who
wants to check out the patreon all tiers
are super appreciated i'm still fairly
new to the youtube game trying to figure
out how to make the patreon worth it to
anyone who wants to subscribe over there
so if you have ideas about that as well
let me know in the comments below make
sure you're subscribed to the channel
leave a like on the video if you watched
all the way through to here you're my
hero
and i just really appreciate everyone
who's supporting it as always good luck
with your projects and thanks for
watching thanks bye