[英]python sqlite3 save value out of spinbox into database by pushing button
Trying to save a value from a spinbox into my database. 尝试将旋转框中的值保存到我的数据库中。 This is a part of my code.
这是我的代码的一部分。
numericupdownLL = tk.Spinbox(self, from_=0, to=300)
numericupdownLL.pack()
def saveDate(title):
vLL = int(numericupdownLL.get()) ## convert to int because output of spinbox= str and database=int
c.execute("UPDATE settings SET ll=? WHERE name=?",(vLL, title))
conn.commit()
buttonSave = tk.Button(self, text='save', command=saveData(something)
buttonSave.pack()
Now I don't get any error, but the code writes always a zero to my db instead of the value of the spinbox. 现在我没有任何错误,但是代码向我的数据库始终写入零,而不是Spinbox的值。
Any idea? 任何想法?
I'll correct the typos in the code in order to be consistent in the explanation 我将更正代码中的拼写错误,以便与说明保持一致
numericupdownLL = tk.Spinbox(self, from_=0, to=300)
numericupdownLL.pack()
# Changed saveDate to saveData that it's actually called in the button
def saveData(title):
# Corrected indentation
vLL = int(numericupdownLL.get())
c.execute("UPDATE settings SET ll=? WHERE name=?",(vLL, title))
conn.commit()
# Added a missing parentheses
buttonSave = tk.Button(self, text='save', command=saveData(something))
buttonSave.pack()
Two notes: 两个注意事项:
So let's start: 因此,让我们开始:
The first thing to note is that you're calling the function only once and not each time the button is clicked. 首先要注意的是,您只调用一次该函数,而不是每次单击该按钮时。 That's because instead of passing the function you're passing a None which is the return of that function call.
那是因为您没有传递函数,而是传递了None,它是该函数调用的返回。 So to fix that you should remove the parentheses and just pass the function as a callback
因此,要解决此问题,您应该删除括号并仅将函数作为回调传递
# WARNING! This code does not work! Keep reading!
buttonSave = tk.Button(self, text='save', command=saveData)
After correcting that each time you click the button an exception will be risen telling you that the function needs 1 argument and 0 is passed . 校正每次单击按钮后,都会引发异常,告诉您该函数需要1个参数并传递0 。 This exception is because of the
title
parameter in the saveData
function so if you want something to be passed as a parameter you will need to return a callback that uses that parameter inside but that it does not accept any parameter: 该异常是由于
saveData
函数中的title
参数,因此,如果您希望将某些内容作为参数传递,则需要返回一个在内部使用该参数但不接受任何参数的回调:
def get_save_data_fn(title):
def saveData():
vLL = int(numericupdownLL.get())
c.execute("UPDATE settings SET ll=? WHERE name=?",(vLL, title))
conn.commit()
return saveData # Returns a function with 0 parameters that uses the given title
# Now you can retrieve the function that takes no arguments and has 'something' used
# in the sql statement
buttonSave = tk.Button(self, text='save', command=get_save_data_fn(something))
So the final code should look like: 因此,最终代码应如下所示:
numericupdownLL = tk.Spinbox(self, from_=0, to=300)
numericupdownLL.pack()
def get_save_data_fn(title):
def saveData():
vLL = int(numericupdownLL.get())
c.execute("UPDATE settings SET ll=? WHERE name=?",(vLL, title))
conn.commit()
return saveData # Returns a function with 0 parameters that uses the given title
# Now you can retrieve the function that takes no arguments and has 'something' used
# in the sql statement
buttonSave = tk.Button(self, text='save', command=get_save_data_fn(something))
buttonSave.pack()
Summarizing: 总结:
something
in the callback you must retrieve a function without parameters and use that variable as a closure. something
回调,你必须检索不带参数的函数,并使用该变量作为一个封闭。 If any part of the code doesn't work for you, let me know! 如果代码的任何部分对您不起作用,请告诉我!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.