Home > Net >  How can I check if an entry in tkinter has one or more zeros
How can I check if an entry in tkinter has one or more zeros

Time:11-21

I'm just starting programming. I'm want to do a simple Ohm's law calculator. I'm struggling to check if the entry is empty or if it has one or more zeros in it. How can I check if it has one or more zeros in it?

from tkinter import *
import math as m

def doMath():
   U = entryU.get()
   I = entryA.get()
   if I == "0" or \
      I == "" or \
      U == "":                                                        
      labelR.config(text="Error", bg= "red")   
   else:                                              
      R = float(U)/float(I)                                       
      labelR.config(text="Resistance: {:.2f}\u03A9".format(R), bg="white")

frame = Tk()
entryU = Entry(frame)
entryU.place (x=95, y= 60, width= 250, height =30)

entryA= Entry(frame)
entryA.place(x=95, y=100, width= 250, height=30)

buttonMath = Button(frame, text="R",command = doMath)
buttonMath.place(x=360, y=70, width=120, height=50)

labelR = Label (frame, text="Resistance: ")
labelR.place(x=10, y= 145) 

frame.mainloop()

That's what I have but as soon as I have "00" in my string this won't work obviously and if I try to just check if I==0 it won't work because I can't compare string with a int (I suppose). I also want to be able to calculate with decimal numbers. Thanks for any help!

CodePudding user response:

If you want to check how many "0" are in your string you can iterate over the string and compare each letter in a string to "0" using for loop.

def check_zeros(string):
    how_many_times = 0
    for l in string:
        if l == "0":
            how_many_times  = 1
    return how_many_times

print(check_zeros("0Lol00xD13202130110"))

CodePudding user response:

In this scenario, rather than check for specific errors in one or the other Entry widgets, that it would be better to just handle the case where either one of them can't be converted into a float for any reason — because that's all the really matters.

If an error occurs trying to do the conversions, then you could try to diagnose the problem and maybe pop-up a messagebox or something with additional error information. However, if you think about it, there are huge number of ways to have entered something invalid, so I'm not sure it would be worth the trouble.

Note that you still might want to check for potential value problems after successful conversion, like I being 0.

Anyway, here's how to implement the simple approach I suggested initially:

from tkinter import *
import math as m

def doMath():
    U = entryU.get()
    I = entryA.get()
    try:
        R = float(U) / float(I)
    except ValueError:
        labelR.config(text="Error", bg= "red")
        return
    labelR.config(text="Resistance: {:.2f}\u03A9".format(R), bg="white")


frame = Tk()
frame.geometry('500x200')
entryU = Entry(frame)
entryU.place(x=95, y=60, width=250, height=30)

entryA = Entry(frame)
entryA.place(x=95, y=100, width= 250, height=30)

buttonMath = Button(frame, text="R", command=doMath)
buttonMath.place(x=360, y=70, width=120, height=50)

labelR = Label(frame, text="Resistance: ")
labelR.place(x=10, y= 145)

frame.mainloop()

CodePudding user response:

You can just split the variable:

def doMath():
    U = entryU.get()
    I = entryA.get()
    splttedI = ''.split(I)
    if "0" in splttedI or \
       I == "" or \
       U == "":                                                        
       labelR.config(text="Error", bg= "red")   
    else:                                              
        R = float(U)/float(I)                                              
        labelR.config(text="Resistance: {:.2f}\u03A9".format(R), bg="white")
  • Related