Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
继续在Python中获取空插入_Python_Sqlite - Fatal编程技术网

继续在Python中获取空插入

继续在Python中获取空插入,python,sqlite,Python,Sqlite,我正在尝试制作一个小程序,将数据保存到sqlite数据库中。问题是我不能插入任何数据,只能插入空数据。我不知道我做错了什么 提前谢谢!这是我的密码 class Vista(Frame): def __init__(self, master): Frame.__init__(self,master) self.grid() self.pack() self.create_widgets() def insertar(

我正在尝试制作一个小程序,将数据保存到sqlite数据库中。问题是我不能插入任何数据,只能插入空数据。我不知道我做错了什么

提前谢谢!这是我的密码

class Vista(Frame):
    def __init__(self, master):
        Frame.__init__(self,master)
        self.grid()
        self.pack()
        self.create_widgets()

    def insertar(self, n, v):
        try:
            cursor.execute('''INSERT INTO passwords (name, value) VALUES (?,?)''', (n.get(), v.get()))
            con.commit()
        except Exception as e:
            # Roll back any change if something goes wrong
            con.rollback()
            print "Error %s;" %e.args[0]
            sys.exit(1)

        finally:
            if con:
                con.close()

    def create_widgets(self):
        self.title_nombre = Label(self, text="Nombre:")
        self.title_nombre.grid(row = 1, column = 1, sticky = W, pady=(10, 0))
        nom = StringVar()
        pas = StringVar()
        self.nombre = Entry(self, textvariable=nom)
        self.nombre.grid(row = 1, column = 2, sticky = W, pady=(10, 0))
        self.title_passwd = Label(self,text="Password:")
        self.title_passwd.grid(row = 2, column = 1, sticky = W)
        self.passwd = Entry(self, textvariable= pas)
        self.passwd.grid(row = 2, column = 2, sticky = W)
        self.boton = Button(text="Añadir", command=self.insertar(nom, pas))
        self.boton.grid(columnspan=1, row=3, sticky = W, pady=(10,0), padx=(5,0))

root = Tk()
root.title("Contraseña")
root.geometry("250x150")

root.eval('tk::PlaceWindow %s center' % root.winfo_pathname(root.winfo_id()))
app = Vista(root)
app.grid(column=0, row=0)
app.columnconfigure(0, weight=1)
app.rowconfigure(0, weight=1)

app.mainloop()

问题在于这一行:

self.boton = Button(text="Añadir", command=self.insertar(nom, pas))
command=self.insertar(nom,pas)
首先调用
self.insertar(nom,pas)
,然后作为该函数调用的结果传递
command
。要将函数传递给
命令

def insert_command():
    return self.insertar(nom, pas)

self.boton = Button(text="Añadir", command=insert_command)
或者在一行中完成:

self.boton = Button(text="Añadir", command=lambda: self.insertar(nom, pas))

问题在于这一行:

self.boton = Button(text="Añadir", command=self.insertar(nom, pas))
command=self.insertar(nom,pas)
首先调用
self.insertar(nom,pas)
,然后作为该函数调用的结果传递
command
。要将函数传递给
命令

def insert_command():
    return self.insertar(nom, pas)

self.boton = Button(text="Añadir", command=insert_command)
或者在一行中完成:

self.boton = Button(text="Añadir", command=lambda: self.insertar(nom, pas))

谢谢,我不知道如何使用第一个,但是第二个很好用。我不明白为什么那样行。@Francisco:他们两个都应该行得通。第二个是第一个的较短版本。谢谢,我不知道如何应用第一个,但第二个很好用。我不明白为什么那样行。@Francisco:他们两个都应该行得通。第二个是第一个的较短版本。