簡體   English   中英

Python-如何驗證Tkinter輸入字段

[英]Python - How to validate tkinter entry field

在我的程序中,我有一個sqlite數據庫,通過tkinter gui中的條目小部件將數據附加到該數據庫。 我想要它,因此數據僅在驗證之后才追加到數據庫中,因為當前沒有驗證。

例如,在我下面的函數中,該函數在我的數據庫的客戶表中附加了一個customerID,姓氏,姓氏,地址和電話號碼。 我想這樣,customerID條目僅接受整數,姓氏,姓氏和地址為NOT NULL,phoneNumberEntry也僅接受整數。

我已經看到人們使用validate命令,但是我認為我已經可以使用將數據追加到數據庫的命令了,所以我無法實現它。

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

這是sql衛生問題還是python編程問題?

如果使用sql衛生,則需要找出要拒絕的sql字符串或字符,可能還存在執行此操作的庫。 https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet

按照編程,一個if語句將運行,從而更改操作順序並使用字符串替換。 http://bobby-tables.com/python.html

在您的代碼中,您似乎需要注意的是有人試圖通過您的字段發布代碼。 仔細看最后一個鏈接。

首先嘗試“ 不要重復自己的自我

# 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM