【问题标题】:Python - How to validate tkinter entry fieldPython - 如何验证 tkinter 输入字段
【发布时间】:2017-03-30 14:14:53
【问题描述】:

在我的程序中,我有一个 sqlite 数据库,其中数据通过 tkinter gui 中的条目小部件附加到其中。我想要它,因此数据仅在验证后才附加到数据库中,因为目前没有验证。

例如,在下面的函数中,它将客户 ID、名字、姓氏、地址和电话号码附加到数据库中的客户表中。我想要它,所以 customerID 条目只接受整数,名字、姓氏和地址不为空,而 phoneNumberEntry 也只接受整数。

我看到人们使用 validatecommand,但我认为我无法实现它,因为我已经在使用命令将数据附加到数据库中。

def appendToCustomerTableEntry(event):
    top = Toplevel()
    top.title("Add to customer table")

    Label(top, text = "customerID: ").grid(sticky = E)

    customerIDEntry = Entry(top)
    customerIDEntry.grid(row = 0, column = 1)

    Label(top, text = "Forename: ").grid(row = 1, sticky = E)

    customerForenameEntry = Entry(top)
    customerForenameEntry.grid(row = 1, column = 1)

    Label(top, text = "Surname: ").grid(row = 2, sticky = E)

    customerSurnameEntry = Entry(top)
    customerSurnameEntry.grid(row = 2, column = 1)

    Label(top, text = "Address: ").grid(row = 3, sticky = E)

    customerAddressEntry = Entry(top)
    customerAddressEntry.grid(row = 3, column = 1)

    Label(top, text = "Phone Number: ").grid(row = 4, sticky = E)

    customerPhoneNumberEntry = Entry(top)
    customerPhoneNumberEntry.grid(row = 4, column = 1)

    exitButton = Button(top, text = "Exit", command = top.destroy)
    exitButton.grid(row = 5, column = 2, sticky = W)

    appendButton = Button(top, text = "Append", command =   lambda:appendToCustomerTable
                  (customerIDEntry.get(), customerForenameEntry.get(), customerSurnameEntry.get(),
                   customerAddressEntry.get(), customerPhoneNumberEntry.get()))
    appendButton.grid(row = 5, column = 1, sticky = E)


def appendToCustomerTable(customerID, Forename, Surname, Address, TelephoneNumber):
    c.execute("INSERT INTO customerTable VALUES (?, ?, ?, ?, ?);", (customerID, Forename, Surname, Address, TelephoneNumber ))
    conn.commit()

【问题讨论】:

  • 您写道:我看到人们使用 validatecommand,但我认为我无法实现它,因为我已经在使用命令将数据附加到数据库中。 你这是什么意思? validatecommand 的使用与你以后如何使用数据完全无关。它只是一种防止非法输入(例如整数字段中的字母)的机制。
  • @BryanOakley 是的,我现在已经想通了。我之前有点困惑,所以就匆匆忙忙地发了这个帖子。现在我有了一种只能接受整数的方法,这样部分问题就解决了,我只需要弄清楚其余的验证
  • 您的问题的答案似乎是简单地创建一个函数来获取所有输入并验证它们,然后在将数据插入数据库之前调用该函数。您要求的与此不同的是什么?

标签: python sqlite validation tkinter


【解决方案1】:

这是 sql 卫生问题,还是 python 编程问题?

如果 sql sanitation,您需要弄清楚要拒绝哪些 sql 字符串或字符,可能还有一些库可以做到这一点。 https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet

以编程方式运行 if 语句,更改操作顺序并使用字符串替换。 http://bobby-tables.com/python.html

在您的代码中,您需要注意的是有人试图通过您的字段发布代码。看看链接,特别是最后一个。

【讨论】:

    【解决方案2】:

    首先尝试“dont repeat your self

    # you can declare here the input type of your argument default and the type of them 
    def build(ui_title = [], int_arg = 0):
        # on top you can also assert the input
        # continue only if ui_title is True else give a AssertionError 
        assert (ui_title), "list is empty!!!"
    
        # lets check int_arg for int
        assert (int_arg==int), "{0} except int get {1}".format(int_arg ,type(int_arg))
    
        for row,text in enumerate(ui_title):
            Label(top, text = str(text)).grid(sticky = E)
            customerIDEntry = Entry(top)
            customerIDEntry.grid(row = int(row), column = 1)
            if text=="Exit":
                exitButton = Button(top, text = str(text), command = top.destroy)
                exitButton.grid(row = int(row), column = 2, sticky = W)
    
    ui_title = ["customerID", "Forename: ", "Surname: ", "Address: ", "Phone Number: ", "Exit"]
    build(ui_title) # will work
    build(ui_title, int_arg = "Hallo") # will not work, because int_arg get string and the build method will raise a AssertionError
    

    【讨论】:

    • 请反馈为什么要投票给唐宁,对未来的读者和我并没有真正的帮助,包括......meta.stackexchange.com/questions/135/…
    • 这没有回答所提出的问题。问题是关于如何验证输入,此代码不会验证用户在 Entry 小部件中输入的数据。
    • 此代码似乎在用户有机会输入任何数据之前检查值。
    猜你喜欢
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    相关资源
    最近更新 更多