parent
fbe9baccb4
commit
27df38732c
@ -0,0 +1,109 @@
|
||||
import tkinter as tk
|
||||
import tkinter.messagebox as msgbox
|
||||
import sqlite3
|
||||
import datetime
|
||||
|
||||
def addquote(string):
|
||||
"""为any加上引号"""
|
||||
return "\'" + str(string) + "\'"
|
||||
|
||||
window = tk.Tk()
|
||||
window.resizable(0, 0)
|
||||
window.title("添加车辆信息")
|
||||
# 窗口居中
|
||||
ww, wh = 300, 300
|
||||
sw, sh = window.winfo_screenwidth(), window.winfo_screenheight()
|
||||
x, y = (sw - ww) / 2, (sh - wh) / 2
|
||||
window.geometry("%dx%d+%d+%d" % (ww, wh, x, y))
|
||||
|
||||
l_Id = tk.Label(window, text="车牌号:")
|
||||
l_Name = tk.Label(window, text="姓名:")
|
||||
l_Color = tk.Label(window, text="颜色:")
|
||||
l_Addr = tk.Label(window, text="住址:")
|
||||
l_Phone = tk.Label(window, text="电话:")
|
||||
|
||||
# 间隔30
|
||||
l_Id.place(x=10, y=20)
|
||||
l_Name.place(x=10, y=50)
|
||||
l_Color.place(x=10, y=80)
|
||||
l_Addr.place(x=10, y=110)
|
||||
l_Phone.place(x=10, y=140)
|
||||
|
||||
# 录入框/获得单行文本框的信息
|
||||
def checkdata():
|
||||
if e_Phone.get() is None: #e_Id
|
||||
return False
|
||||
try:
|
||||
int(e_Phone.get())
|
||||
return True
|
||||
except ValueError:
|
||||
msgbox.showwarning(message="非法输入")
|
||||
return False
|
||||
|
||||
# 检查字符串是否由数字组成
|
||||
def check_value(string):
|
||||
try:
|
||||
int(string)
|
||||
return True
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
var_id = tk.StringVar()
|
||||
var_name = tk.StringVar()
|
||||
var_color = tk.StringVar()
|
||||
var_addr = tk.StringVar()
|
||||
var_phone = tk.StringVar()
|
||||
|
||||
e_Id = tk.Entry(window, width=30, textvariable=var_id)
|
||||
e_Name = tk.Entry(window, width=30, textvariable=var_name)
|
||||
e_Color = tk.Entry(window, width=30, textvariable=var_color)
|
||||
e_Addr = tk.Entry(window, width=30, textvariable=var_addr)
|
||||
e_Phone = tk.Entry(window, width=30, textvariable=var_phone,validate="focusout", invalidcommand=checkdata)
|
||||
|
||||
# 间隔30
|
||||
e_Id.place(x=60, y=20)
|
||||
e_Name.place(x=60, y=50)
|
||||
e_Color.place(x=60, y=80)
|
||||
e_Addr.place(x=60, y=110)
|
||||
e_Phone.place(x=60, y=140)
|
||||
|
||||
# 按钮
|
||||
def func_save():
|
||||
# 检查车牌号是否为空,为空则报错 通过entry属性实现
|
||||
# todo
|
||||
if e_Id.get() == "":
|
||||
msgbox.showwarning(message="车牌号为空")
|
||||
else:
|
||||
ids, name, color, addr, phone = var_id.get(), var_name.get(), var_color.get(), var_addr.get(), var_phone.get() # 获得输入的文本内容
|
||||
con = sqlite3.connect("./car_info.db") # 连接数据库
|
||||
cur = con.cursor() # 创建游标对象
|
||||
|
||||
# 获取当前时间
|
||||
current_time = datetime.datetime.now()
|
||||
current_time_str = current_time.strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
# 尝试写入,如果遇到重复则报错,确认是否需要修改
|
||||
try:
|
||||
cur.execute('insert into car values(?, ?, ?, ?, ?, ?)', (ids, name, color, addr, phone, current_time_str))
|
||||
msgbox.showinfo(message="添加成功!")
|
||||
window.quit()
|
||||
except sqlite3.IntegrityError:
|
||||
msgbox.showerror("⚠", "车牌号重复")
|
||||
res = msgbox.askyesno("⚠", "是否需要覆盖")
|
||||
if res is True:
|
||||
res2 = msgbox.askokcancel("⚠", "一旦确认则无法撤销,请确认")
|
||||
if res2 is True:
|
||||
cur.execute("update car set name=?, color=?, addr=?, phone=?, stay_hours=? where id=?", (name, color, addr, phone, current_time_str, ids))
|
||||
# else:
|
||||
# window.quit()
|
||||
|
||||
con.commit()
|
||||
cur.close()
|
||||
con.close()
|
||||
|
||||
btn_save = tk.Button(window, text="保存", width=10, command=func_save)
|
||||
btn_exit = tk.Button(window, text="退出", width=10, command=window.quit)
|
||||
btn_save.place(x=110, y=220)
|
||||
btn_exit.place(x=200, y=220)
|
||||
|
||||
window.mainloop()
|
Loading…
Reference in new issue