在我的程序中,我有一個sqlite數據庫,其中數據通過tkinter gui中的輸入小部件附加到數據庫。我喜歡它,所以數據只在數據庫驗證後附加到數據庫中,因爲目前沒有驗證。Python - 如何驗證tkinter輸入字段
例如,在我的功能在其下面我的數據庫追加的customerID,名,姓,地址和電話號碼,以客戶表。我喜歡它,所以customerID條目只接受整數,forename,surname和address爲NOT NULL,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()
您寫道:_I看到人們使用validatecommand,但我不認爲我將能夠實現,由於我已經在使用一個命令來將數據追加到數據庫._你是什麼意思? 'validatecommand'的使用與以後如何使用數據完全無關。它只是一種防止非法輸入的機制(如整數字段中的字母)。 –
@BryanOakley是的,我現在已經想出了一些東西。剛纔我只是有點困惑,所以才衝上這篇文章。現在我已經有了一種方法,只能接受整數,以便部分問題得到解決,我只需要弄清楚其餘的驗證 – JoeW373
這似乎是你的問題的答案是簡單地創建一個函數,它需要所有輸入並驗證它們,然後在將數據插入數據庫之前調用該函數。你要求什麼與此不同? –