[英]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
下面是修改后的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.