首页 > 解决方案 > Tkinter 按钮自动点击

问题描述

我正在尝试编写一个自动生成按钮的简单应用程序,并通过单击生成的按钮将它们的值保存到数据库表中。出于某种原因,我不明白在生成按钮时好像它们会自动单击保存所有值。谁能告诉我为什么?

try:
    conexion=sqlite3.connect('libreria.db')
    cursor=conexion.cursor()
    cursor.execute("SELECT * FROM categoria")
    cate=cursor.fetchall()
except sqlite3.OperationalError:
    print("Error: No hay datos que mostrar")
else:
    eleccion=lambda x: cursor.execute("INSERT INTO pedido VALUES(null,{})".format(x))
    for c in cate:
        print("\n*",c[1],"*")
        ttk.Label(marcoprincipal,text="*{}*".format(c[1])).pack(fill="both",expand=1)
        libros=cursor.execute("SELECT * FROM libro WHERE categoria_id={}".format(c[0])).fetchall()
        for l in libros:
            print("\t",l[1])
            ttk.Button(marcoprincipal,text="{}".format(l[1]),command=eleccion(l[0])).pack()
            conexion.commit()


    conexion.commit()
    conexion.close()

标签: python-3.xsqlitetkinter

解决方案


推荐阅读