简体   繁体   English

如何修复我的错误处理代码? (tkinter 图形用户界面)

[英]how to fix my Error handling code? (tkinter GUI)

I have split the error handing into 3 parts, ( check_black, raise_error, delete_error)我已将错误处理分为 3 个部分(check_black、raise_error、delete_error)

About about the functions:关于功能:

the check_blank() function checks if there are any errors in the input entries and raise_error() function raises the error by gridding() a ErrorLabel. check_blank() function 检查输入条目中是否有任何错误, raise_error() function 通过 gridding() 一个 ErrorLabel 引发错误。 and if the error have been resolved then delete_error() function deletes the error label.如果错误已解决,则 delete_error() function 删除错误 label。

(there are 5 entry boxes and 4 check buttons, and a 'done' button) ( error should be raised if there's no input for all 5 Entries or at least one check button hasn't been checked ) (有 5 个输入框和 4 个复选按钮,以及一个“完成”按钮)(如果所有 5 个条目都没有输入或至少一个复选按钮未被选中,则应引发错误)

this is the check_blanks() function:这是 check_blanks() function:

 def checkblanks(self):
    flag = 0

    if not self.Text1.get():   #text.get() and Answer1.get() etc stores input from entry so if there's no entry then it will be empty
        flag = 1
    if not self.Answer1.get():
        flag = 1
    if not self.Answer2.get():
        flag = 1
    if not self.Answer3.get():
        flag = 1
    if not self.Answer4.get():
        flag = 1
    if not self.var.get():    # var.get stores input from check button so if its not checked then there won't be anything stored in var.get()
        flag += 2

    if flag == 0:     # flag = 0 means no error
        self.ErrorLabel = None

        self.add() #### EDIT ####
        self.delete_error()   
    elif flag == 1 or flag == 2 or flag == 3:   # if flag is 1 or 2 or 3 means there is error
        self.raise_error(flag)
 flag 0 = means no errors flag 1 = means that there's no input in all 5 entries flag 2 = means at least one check button hasn't been checked flag 3 = means both above errors occurred

this is the raise error_function:这是引发错误函数:

 def raise_errors(self, flag):
    if flag == 1 or flag == 3:
        self.L5.grid_forget()
        self.ErrorLabel = tk.Label(self.frame2, text="Error: Fill all the blanks !", fg='white', bg='red')
        self.ErrorLabel.grid(row=7, column=0, pady=10)
    elif flag == 2:
        self.L5.grid_forget()
        self.ErrorLabel = tk.Label(self.frame2, text="Error: check ✔️ the correct answer ", fg='white', bg='red')
        self.ErrorLabel.grid(row=7, column=0, pady=10)

and the delete_error function:和 delete_error function:

   def delete_error(self):
    if self.ErrorLabel is not None:   # checks if the error label exists  
        self.ErrorLabel.grid_forget()

but there is a problem:但有个问题:

When the error is raised and then error label is made;当出现错误,然后出现错误 label; after the errors are resolved then the error label is not deleted by delete_error()错误解决后,delete_error() 不会删除错误 label

and sometimes multiple error labels overlap each other and it not very efficient有时多个错误标签相互重叠,效率不高

I need to make the error handling functions more simple and work properly.我需要使错误处理功能更简单并正常工作。

EDIT:编辑:

also the function add() is called if flag == 0,( if there's no errors ) the add() function just creates a new frame with the same entries and check buttons its and checks for errors all over again. function add() 如果 flag == 0 被调用,(如果没有错误) add() function 只是创建一个具有相同条目的新框架并检查其按钮并再次检查错误。

its just a repeat loop它只是一个重复循环

this is that function:这是function:

    def add(self):
        self.X += 1 
        self.frame2.grid_forget()    # deleted main frame
        self.amount_of_questions -= 1
        self.question(self.X)      # question() creates new frame with same widgets

It is over writing each label because you are creating new label for each function call and so on, so I think its safe for us now, to get rid of None and try to use config :每个 label 都写完了,因为您正在为每个 function 调用创建新的 label 等等,所以我认为它现在对我们来说是安全的,摆脱None并尝试使用config

def checkblanks(self):
    flag = 0
.....

    self.ErrorLabel = tk.Label(self.frame2,fg='white',bg='red') # Define the variable with basic base options
    if flag == 0:
        self.delete_error()   
    elif flag == 1 or flag == 2 or flag == 3:
        self.raise_error(flag)

and then delete_error() would be:然后delete_error()将是:

def delete_error(self):
    self.ErrorLabel.grid_forget() # Now always errorlabel is defined, you might want to use winfo_exists

So now you have to use config to update the widget inside raise_errors() :所以现在你必须使用config来更新raise_errors()内的小部件:

def raise_errors(self, flag):
    if flag == 1 or flag == 3:
        self.L5.grid_forget()
        self.ErrorLabel.config(text="Error: Fill all the blanks !") # Update the widget
        self.ErrorLabel.grid(row=7, column=0, pady=10) # Grid it
    elif flag == 2:
        self.L5.grid_forget()
        self.ErrorLabel.config(text="Error: check ✔️ the correct answer ") # Update the widget
        self.ErrorLabel.grid(row=7, column=0, pady=10) # Grid it

Since I don't have a code to test this up with, this is based on imagination, I think you need winfo_exists inside delete_errors() too.由于我没有代码来测试它,这是基于想象,我认为您也需要在winfo_exists delete_errors()中使用 winfo_exists 。

after the errors are resolved then the error label is not deleted by delete_error()错误解决后,delete_error() 不会删除错误 label

This might be due to the way self.ErrorLabel was defined as None .这可能是由于self.ErrorLabel被定义为None的方式。 Now that it is always defined as a Label , it should be fine.现在它总是被定义为Label ,应该没问题。

EDIT: Can you try moving the label inside of __init__ :编辑:您可以尝试将 label 移动到__init__内:

def __init__(self):
    ....

    self.ErrorLabel = tk.Label(self.frame2,fg='white',bg='red')

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

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