Python Forum
[Tkinter] function to handle exception errors
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] function to handle exception errors
#1
Hello;
I created an application with "TKINTER" which works well today according to my weak knowledge since I am a beginner with the PYTHON language (version 3.5) under Windows 10 64 bits.
It prevents me to finalize this version 1 of this application that I would like to develop even better in the near future. here is the sample code of the application that concerns exception errors problem :

def message_erreur():
    if ent_labo_loc.get()==StringVar:
        lb_loc_exception['text']='Erreur, veuillez introduire seulement des chiffres'
        ent_labo_loc.set("")
    else:
        lb_loc_exception['text']=''
     
def afficher_resultat_loc():
 
    try :              
        calcul=20*log10(float(ent_labo_loc.get())/float(entree_ref_loc.get()))
        calcul=round(calcul,3)
        ent_correction_loc.set(calcul)
        ent_labo_loc.set("")
    except :                
        message_erreur()
    finally :
        pass
the function " afficher_resultat_loc() " is attached to the validation button of the mathematical calculation. Here is the code :

#======= bouton de validation de la correction =====
 
bt_loc_validation=Button(loc,
                     text='Valider',
                     bd=14,             
                     relief=RIDGE,
                     font=('arial',16,'bold'),
                     justify='left',
                     command=afficher_resultat_loc)
bt_loc_validation.place(x=420,y=460)
finally I created a text which must be displayed each time we introduce characters instead of numbers by a label whose code is:

#affichage d'un message en cas d'erreur exception
 
lb_loc_exception=Label(loc,text='',
                   fg='red',
                   font=('arial',18,'bold'),
                   justify='left',
                   bg='cadet blue'
                   )
 
lb_loc_exception.place(x=30,y=420)
the text that will be integrated in this label only appears when the user writes characters and must imperatively to be deleted when the use after introduces a decimal number

I hope I have detailed my problem

thanks for the help
Reply
#2
you should only capture expected exceptions, If you're not sure what is getting thrown,
you can temporarily:
import sys
# add after line 15
        print("Unexpected error:", sys.exc_info()[0])
        raise
But once you're confident of the exceptions that you are getting, you should
change the except clause to be: 'except ValueError:' for example
Reply
#3
hello;

here is the picture of my application. I have indicated where the error message should be set and which should be erased in case of correction of input data (enter numbers instead of characters)

[Image: 180816082220535645.jpg]

thanks for help
Reply
#4
hello;
for more information this picture describe what I want to do for handling exception:

I describe my problem explicitly: when the user writes a string in the "Entry" widget which carries the instruction "enter the value of the DDM read by the plane LABO". this displays the red alert message below the Entry widg (for example you have introduce cataracts and not numbers, please enter decimal numbers). Then once it enters a decimal correct number it appears the result indicates in the block of the try, and at the same time the red message of alarm must be erased.

[Image: 180818125318243377.jpg]

thanks for help
Reply
#5
(Aug-15-2018, 10:20 PM)atlass218 Wrote: I hope I have detailed my problem
It is not that clear. Which part doesn't work in your code?

By the way, an entry validation mechanism exists in tkinter see here. It would be a good idea to use it.
Reply
#6
hi;
I want that the error message will be writed when the user introduce charater in entry, and if after the user introduce decimal numbers intead of characters the error message
wil be deleted
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020