数据验证如何避免字母输入整数输入框

时间:2016-12-08 22:05:30

标签: python-3.x validation user-interface subroutine

我的仓库库存库存控制功能允许用户输入他们从货架上取下的数量。如果他们输入的值超过文件中保存的实际数量或输入字母,我想产生错误。

Amt_Taken_Submit = Button(root_stock,
                          text = "Enter",
                          fg = "blue",
                          command = lambda:Stock_Check(pntext),
                          bg = "grey",
                          font = "Helevetica 10 bold",
                          height = "1",
                          width = "10",
                          ).grid(row = 8,column = 4)

这是按钮本身的代码。

def Stock_Check(pntext):
    test = No_Quantity_Change.get()
    if test == 0:#To Allow the user to check stock levels without having to take anything away.
        root_stock.destroy()
    else:
        master = []#Array in which data will be stored
        fieldnames = ["Part Number",
                    "Prod Desc 1",
                    "Prod Desc 2",
                    "Prod Desc 3",
                    "Prod Desc 4",
                    "Price",
                    "ReOrder Levels",
                    "Warehouse Location",
                    "Quantity"]
    with open("PN_Database.csv", "r") as csvfile:
        Data_File = csv.DictReader(csvfile, delimiter = ",")
        for row in Data_File:
            master.append(row)
    csvfile.close()

    for row in master:
        if row["Part Number"] == pntext:
            string = PartInfoString.get()
            index = string.index('Quantity: ') + 10#'+10 to indicate to the program that hte actual figure is after 10 characters.
            row["Quantity"] = int(string[index:]) - int(No_Quantity_Change.get())
            PartInfoString.set(row['Part Number']+ "\n"
                                + row['Prod Desc 1']+ "\n"
                                + row['Prod Desc 2']+ "\n"
                                + row['Prod Desc 3']+ "\n"
                                + "Colour: " +row['Prod Desc 4']+ "\n"
                                + "Price :" + row['Price']+ "\n"
                                + "Re-Order Level :" + row['ReOrder Levels']+ "\n"
                                + "Location :" + row['Warehouse Location'] + "\n"
                                + "Quantity: " + str(row['Quantity']))

    with open("PN_Database.csv", "w") as csvfile:
        Data_File = csv.DictWriter(csvfile, fieldnames=fieldnames)
        Data_File.writeheader()
        for row in master:
            Data_File.writerow(row)
    csvfile.close()

这是按下按钮时启动的子程序..

这是我想要通过错误启动的错误窗口。     def Error_Win():     错误= Tk()     error.title("某事错了")     error.configure(bg =" black")

Error_Msg = Label(error,
                  text = """
Error!
The part number that you
have entered is incorrect,
please try again""",
                  fg = "Blue",
                  bg = "black",
                  font = "Helevetica 15 bold"
                  ).grid(row = 0, column = 0, sticky = NW, pady = 20, padx = 50)


"""Return_Btn = Button(error,
                    text = "Return to Main Menu",
                    fg = "Blue",
                    bg = "Grey",
                    command =
                    ).grid(row = 1, column = 0, sticky = N, pady = 10)"""

error.mainloop()

0 个答案:

没有答案