Tkinter命令Lambda函数修改变量 [英] Tkinter Command Lambda Function Modify Variable
问题描述
我的应用程序是一种从数据库填充其字段的表单.我正在编写在这些记录之间导航的函数.我在编写下一条记录"功能时遇到问题.
My application is a form which populates its fields from a database. I am in the process of writing the function that navigates between these records. I am having problems writing my "next record" function.
在任何给定时间,屏幕上显示的当前记录"都在变量 current_record
中.
At any given time, the "Current Record" showing on the screen is in a variable current_record
.
def next_record(current_record):
current_index = current_record.index
current_record = Competitor(competitors[current_index + 1], current_index + 1)
print(current_record.index)
populate_form(current_record)
然后我的按钮调用此函数:
And my button calling this function:
action_button_6 = tkinter.Button(group_buttons, text='>>', width=5,
command=lambda: next_record(current_record))
尽管该功能完成了工作并将新记录加载到表单中,但并未如功能第三行所示将"current_record"重新分配给新竞争对手.
Although the function does its job and loads the new record into the form, it does not reassign "current_record" to the new competitor as show in the third line of the function.
如何在此函数中修改变量?
How can I modify the variable from within this function?
推荐答案
除非您明确声明在全局函数中修改的变量,否则即使存在具有相同名称的全局变量,因此 current_record
是局部变量.将 global current_record
添加到 next_record
函数的开头,并将函数参数重命名为 old_record
即可解决此问题.
Unless you explicitly declare a variable that you modify in a function global, it defaults to being local even if there is a global variable with the same name, thus current_record
is a local variable. Adding global current_record
to the beginning of the next_record
function and renaming the function argument to old_record
would fix that.
这篇关于Tkinter命令Lambda函数修改变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!