簡體   English   中英

Python Tkinter 循環在 INSERT SQLITE 之前驗證輸入

[英]Python Tkinter Loop to validate entry before INSERT SQLITE

我試圖在將該信息插入 SQLITE 數據庫之前驗證條目。 我有一個帶按鈕的主 Tkinter 畫布。 在這種情況下,該按鈕會打開另一個窗口,用戶可以在其中輸入所需的字段。 我的想法是避免用戶提交空字段。 我設法有一個消息框說出現了問題,但是一旦我按“確定”,代碼就會繼續並關閉輸入窗口。 步驟 1 - 打開輸入窗口:

def open_add_ship_window():
    global ship_window
    ship_window = Tk()
    ship_window.title('Add Ship')
    ship_window.iconbitmap(
        'C:/Users/eduardo.js.ramos/Desktop/VSCode/img directory/ship_icon_1.ico')
    ship_window.geometry("400x200")
    global ship_name_ship_window
    global ship_imo_ship_window
    global ISM_ship_window
    global ISM_IMO_ship_window
    # Create Text Boxes
    ship_name_ship_window = Entry(ship_window, width=40)
    ship_name_ship_window.grid(row=0, column=1, pady=(10, 0))
    ship_imo_ship_window = Entry(ship_window, width=40)
    ship_imo_ship_window.grid(row=1, column=1)
    ISM_ship_window = Entry(ship_window, width=40)
    ISM_ship_window.grid(row=2, column=1)
    ISM_IMO_ship_window = Entry(ship_window, width=40)
    ISM_IMO_ship_window.grid(row=3, column=1)
    # Create Labels
    ship_name_ship_window_label = Label(
        ship_window, text="Vessel Name")
    ship_name_ship_window_label.grid(row=0, column=0, pady=(10, 0))
    ship_imo_ship_window_label = Label(
        ship_window, text="Vessel IMO")
    ship_imo_ship_window_label.grid(row=1, column=0)
    ISM_ship_window_label = Label(ship_window, text="ISM Company")
    ISM_ship_window_label.grid(row=2, column=0)
    ISM_IMO_ship_window_label = Label(
        ship_window, text="ISM IMO Number")
    ISM_IMO_ship_window_label.grid(row=3, column=0)
    # Create Save New Ship Button
    save_ship_btn = Button(
        ship_window, text="Add New Ship", command=add_ship)
    save_ship_btn.grid(row=4, column=0, columnspan=2,
                       pady=10, padx=10, ipadx=50)

步驟 2 - 將數據提交到 SQLITE 數據庫:

def add_ship():
    # connect to database
    conn = sqlite3.connect('PSC.sdb')
    # create cursor
    c = conn.cursor()
    while True:
        if len(ship_name_ship_window.get()) != 0 \
                or len(ship_imo_ship_window.get()) != 0 \
                or len(ISM_ship_window.get()) != 0 \
                or len(ISM_IMO_ship_window.get()) != 0:
            c.execute("INSERT INTO ships VALUES(:name, :imoship, :ism, :ismimo , null, null)",
                      {
                          'name': ship_name_ship_window.get(),
                          'imoship': ship_imo_ship_window.get(),
                          'ism': ISM_ship_window.get(),
                          'ismimo': ISM_IMO_ship_window.get()
                      })
        else:
            messagebox.showwarning(title='Review Needed', message='Please complete all fieds.')
            continue
    conn.commit()
    conn.close()
    ship_window.destroy()

大家有什么建議嗎?

  • 您應該將add_ship()的最后三行移動到if塊中
  • if語句中更改or and
  • 刪除while循環

下面是修改后的add_ship()

def add_ship():
    # get the information
    ship_name = ship_name_ship_window.get().strip()
    ship_imo = ship_imo_ship_window.get().strip()
    ism_ship = ISM_ship_window.get().strip()
    ism_imo = ISM_IMO_ship_window.get().strip()
    # if all is input
    if ship_name and ship_imo and ism_ship and ism_imo:
        # connect to database
        conn = sqlite3.connect('PSC.sdb')
        # create cursor
        c = conn.cursor()
        c.execute("INSERT INTO ships VALUES (:name, :imoship, :ism, :ismimo, NULL, NULL)",
                  {
                      'name': ship_name,
                      'imoship': ship_imo,
                      'ism': ism_ship,
                      'ismimo': ism_imo
                  })
        conn.commit()
        conn.close()
        ship_window.destroy()
    else:
        messagebox.showwarning(title='Review Needed', message='Please complete all fieds.')

消息框關閉后,主循環結束,因此您可能必須將上述代碼放入循環中,以便一旦消息框顯示錯誤,循環就不會結束。

一種潛在的方式可能是:

While True :
"""Input tags here"""
if len(ship_name_ship_window.get()) != 0 \
                or len(ship_imo_ship_window.get()) != 0 \
                or len(ISM_ship_window.get()) != 0 \
                or len(ISM_IMO_ship_window.get()) != 0:
            c.execute("INSERT INTO ships VALUES(:name, :imoship, :ism, :ismimo , null, null)",
                      {
                          'name': ship_name_ship_window.get(),
                          'imoship': ship_imo_ship_window.get(),
                          'ism': ISM_ship_window.get(),
                          'ismimo': ISM_IMO_ship_window.get()
                      })
            break
else:
     messagebox.showwarning(title='Review Needed', message='Please complete all fieds.')
     continue

暫無
暫無

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

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