python - How to pass different commands in tkinter based on user input -


i'm starting learn python , thought idea learn tkinter. i'm doing program takes 3 numbers user input , calculate them, printing result.

from tkinter import * root = tk() root.title("test") e1 = entry(root) e1.pack() e2 = entry(root) e2.pack() e3 = entry(root) e3.pack() l = label(root) l.pack()  def my_function(a,b,c):     if condition:         (calculations)         l.config(text="option1")     else:         (calculations)         l.config(text="option2")  b = button(root, text="result", command= lambda: my_function(float(e1.get()),float(e2.get()),float(e3.get()))) 

my question is, how can set button print error message in case inputs not numbers? when try inside function,

valueerror: cannot convert string float  

i managed make work despite still printing error in shell using

def combine_funcs(*funcs):     def combined_func(*args, **kwargs):         f in funcs:             f(*args, **kwargs)     return combined_func def checknumber():     if not isinstance(e1.get(),float) or not ...(same others):         l.config(text="only numbers") b = button(root, text="result", command= combine_funcs(checknumber, lambda: my_function(float(e1.get()),float(e2.get()),float(e3.get())))) 

is there easier way not give me error? thanks

use try-except instruction:

try:     # code     b = button(root, text="result", command= lambda: my_function(float(e1.get()),float(e2.get()),float(e3.get())))  except valueerror:     # if catches exception  valueerror     b = button(root, text="only numbers") 

more exception handling


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -