IF-ELIF语句嵌套

时间:2016-03-04 00:54:33

标签: python python-2.7 tkinter tkmessagebox

我想知道是否有更好的写作方法。下面列出的当前代码有效。我只是想知道是否有更好的方法。

这是用于验证。因此if语句检查所有必填字段,如果它们是可接受的,那么它将转到执行代码。该问题的开始是因为

的输入字段
  

self.text_fmax

未被接受为整数。所以这个特定的字段,首先检查是否为空,然后忽略,如果不是,那么该值必须是0到180之间的整数。

  def call_back(self):
    if len(self.text_n.get()) == 0:
        tkMessageBox.showinfo("Mandatory Information", "Please input an integer value for Number of Tessellations Cells")
    elif len(self.text_id.get()) == 0:
        tkMessageBox.showinfo("Mandatory Information", "Please input an integer value for Tessellation Identifier")
    elif len(domain_container) == 0:
        tkMessageBox.showinfo("Mandatory Information", "Please input Domain")
    elif len(self.text_fmax.get()) != 0:
        a = int(self.text_fmax.get())
        if a < 0 or a > 180:
            tkMessageBox.showinfo("Incorrect Value", "Face Flatness should be less than 180")
        elif len(filename4) == 0:
            tkMessageBox.showinfo("Mandatory Information", "Please input Output File Name")
        else:
            self.execute_neper_code()
    elif len(filename4) == 0:
        tkMessageBox.showinfo("Mandatory Information", "Please input Output File Name")
    else:
        self.execute_neper_code()

2 个答案:

答案 0 :(得分:2)

如果您想避免重复的代码和字符串,可以尝试以下方法。 下面不是完整的代码,而是简单的例子。键入t_msgs dict表示要验证的字段和要验证的值(可以是范围)。

def call_back(self):
    t_msgs = {"mdt": "Mandatory Information",
              "incrt_val" :"Incorrect Value"}
    checkFields = {(self.text_n, 0): 
                    (t_msgs["mdt"], 
                     "Please input an integer value for Number of Tessellations Cells"),
                   (self.text_id, 0): 
                    (t_msgs["mdt"], 
                     "Please input an integer value for Tessellation Identifier"),
                   (domain_container, 0): 
                    (t_msgs["mdt"], 
                     "Please input Domain"),
                   (self.text_fmax, range(0, 181, 180)): 
                    (t_msgs["incrt_val"], 
                     "Face Flatness should be less than 180"),
                   (len(filename4), 0): 
                    (t_msgs["mdt"], 
                     "Please input Output File Name")}

    for field in checkFields:
        if not field[1]:
            tkMessageBox.showinfo(checkFields[field][0], checkFields[field][1])
        else:
            if not (field[1][0] < field[0].get() < field[1][1]):
                tkMessageBox.showinfo(checkFields[field[0]], checkFields[field][1])

答案 1 :(得分:1)

您通常不需要检查len == 0,只需测试对象“真实性”。

def call_back(self):
    show = tkMessageBox.showinfo
    if not self.text_n.get():
        show("Mandatory Information", "Please input an integer value for Number of Tessellations Cells")

    elif not self.text_id.get():
        show("Mandatory Information", "Please input an integer value for Tessellation Identifier")

    elif not domain_container:
        show("Mandatory Information", "Please input Domain")

    elif self.text_fmax.get() and not 0 <= int(self.text_fmax.get()) <= 180:
        show("Incorrect Value", "Face Flatness should be less than 180")

    elif not filename4:
        show("Mandatory Information", "Please input Output File Name")

    else:
        self.execute_neper_code()