我的目标是使用Tkinter的复选框将新记录添加到数据库中。例如,如果您选择复选框的值和元素名称之一及其价格,那么它将被插入数据库。
我建立了一个窗口,其中包含几个复选框的值,以及元素名称和价格的输入标签。另外,我尝试编写一个将数据插入数据库的函数,但是记录没有出现在数据库中。
edited_equipment = StringVar()
edited_price = IntVar()
def alter_eqiupment():
master = Tk()
master.geometry("150x350")
navigation_var = IntVar()
Checkbutton(master, text="navigation",
variable=navigation_var).grid(row=0, column = 0, sticky=W)
interior_var = IntVar()
Checkbutton(master, text="interior", variable=interior_var).grid(row=1,
column = 0, sticky=W)
wheels_var = IntVar()
Checkbutton(master, text="wheels", variable=wheels_var).grid(row=2,
column = 0, sticky=W)
audio_var = IntVar()
Checkbutton(master, text="audio", variable=audio_var).grid(row=3,
column = 0, sticky=W)
upholstery_var = IntVar()
Checkbutton(master, text="upholstery",
variable=upholstery_var).grid(row=4, column = 0, sticky=W)
sensor_var = IntVar()
Checkbutton(master, text="sensor", variable=sensor_var).grid(row=5,
column = 0, sticky=W)
Label(master, text = "name", font = ('',13),pady=5,padx=5).grid(sticky
= W)
Entry(master, textvariable = edited_equipment, bd = 3,font =
('',10)).grid(row=7,column=0)
Label(master, text = "price", font = ('',13),pady=5,padx=5).grid(sticky
= W)
Entry(master, textvariable = edited_price, bd = 3,font =
('',10)).grid(row=9,column=0)
def insert_edited():
conn = sqlite3.connect('Baza_trzecia.db')
with conn:
cur = conn.cursor()
if navigation_var.get() == 1:
cur.execute('insert into navigation (name,
price) values (?,?)', ((edited_equipment),
(edited_price))
conn.commit()
# the same if statements for other elements
Button(master, text = ' Save ',bd = 3 ,font = ('',12),padx=10,pady=10,
command = insert_edited).grid()
mainloop()
这是我用SQLAlchemy编写的导航表类:
class Navigations(Base):
__tablename__ = "navigation"
id_navigation = Column(Integer, primary_key = True)
navigation_name = Column(String)
price = Column(Integer)
sqlite_autoincrement=True
def __init__(self, navigation_name, price):
self.navigation_name = navigation_name
self.price = price