繁体   English   中英

如何在同一循环中添加错误警报然后返回主 function

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

在用户搜索 excel 中未找到的值并显示错误弹出后,如何返回用户输入以在 tkinter 中重试?

在我提交的 function 下面它适用于有效条目(excel 文件中的值),但如果输入的值在 excel 文件中找不到,则在正确输入有效值时会继续显示错误,但会继续显示错误对于任何后续搜索

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()

我现在意识到出了什么问题,我在 for 循环之外还需要第二个 if 语句,也不需要 break 语句

对于弹出警告,使用from tkinter import *您已加载 tkinter 的所有tkintermessagebox未导入

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 ?")

第二个问题你应该学习错误和异常
假设我们要提取类似数据的计数
并跳过每个没有喜欢关键字的记录数据

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)

如果有问题想在错误数据后抛出错误

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}")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM