How do you add an error alert then return to main function within same loop

Question:

How do you return to user input to try again in tkinter after the user searches for a value in excel that is not found whereby an error pop up is displayed?

In my submit function below it works for valid entries (values in the excel file) but if a value is entered that is not found in the excel sheet it correctly displays the error but then it continues to display the error when a valid value is entered for any subsequent searches

import tkinter
from tkinter import *
import tkinter as tk
from tkinter import scrolledtext
import openpyxl
from tkinter import messagebox


main = Tk()
main.title("Customer Search App")
main.geometry("1000x600")
main.configure(bg='blue')

excel_path = r".Customer_Lookup.xlsx"

     
def submit():

        search_id = service_id.get()

           
        file = openpyxl.load_workbook(excel_path)

        sheet = file['Sheet1']


        for cell in sheet.iter_rows(min_row=1, max_row=sheet.max_row, 
            min_col=1, max_col=15, values_only=True):


            if cell[0] == search_id:
                date.insert(0, cell[1])
                account.insert(0, cell[2])
                name.insert(0, cell[3])
                comments.insert(1.0, cell[4])

            else:
                tk.messagebox.showerror("Error", "Service ID not found")


                break







main.mainloop()
Asked By: gjc75

||

Answers:

I now realise what is wrong I needed a second if statement outside of the for loop also without needing a break statement as well

Answered By: gjc75

for pop up warning use be careful when use from tkinter import * you have loaded all class of tkinter, messagebox not import

from tkinter import messagebox
#warning
messagebox.showinfo(title="warning" ,message="please make sure haven't left any field empty !")
# confirmation
is_ok=messagebox.askokcancel(title=website,message=f"these are details entered : website:{website}n Email:{email} n Password : {password} n is it ok for save ?")

and second problem you should be learned Errors and Exceptions
assume we want to extract count of like a data
and skip each record data that’s don’t have likes keyword

facebook_posts = [
    {'Likes': 21, 'Comments': 2},
    {'Likes': 13, 'Comments': 2, 'Shares': 1},
    {'Likes': 33, 'Comments': 8, 'Shares': 3},
    {'Comments': 4, 'Shares': 2}, # dont have likes data
    {'Comments': 1, 'Shares': 1},#dont have likes data
    {'Likes': 19, 'Comments': 3}
]
total_likes = 0
for post in facebook_posts:
    try:
         total_likes = total_likes + post['Likes']
    except KeyError:
         pass

print(total_likes)

if in issue want to throw error after wrong data

def bmi():
    height=float(input("inter your height meter ? : "))
    if height>3:
        raise ValueError("your height shouldn't be over 3 meter")
    weight=float(input("inter your weight kg ? : "))
    bmi=weight/height**2
    bmi=round(bmi,2)
    print(f"your bmi is {bmi}")
Answered By: hossein sh
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.