这些问题的答案:How to pass arguments to a Button command in Tkinter?和Why is Button parameter “command” executed when declared?非常感谢。
但是,当我按下按钮以在按下 Enter 键时自动执行时,它会立即执行。我的代码是:
from Tkinter import *
import ttk
from timeAttendance_show_DTR_GUI import *
def employee_toShow(leftFrame,rightFrame):
empNumberToShow = '1'
beginDateToShow = '2014-06-01'
endDateToShow = '2014-06-31'
requiredReport='dtr'
emp = IntVar() #please don't mind yet other parts as I am still working on them bit by bit.
ttk.Label(leftFrame, text='Enter Employee Number').pack()
emp_entry = ttk.Entry(leftFrame, textvariable=emp)
emp_entry.pack()
emp_DTR = ttk.Button(leftFrame, text='Calculate', command=lambda: indiv_DTR(rightFrame, empNumberToShow, beginDateToShow, endDateToShow, requiredReport))
emp_entry.focus()
emp_DTR.pack()
root.bind('<Return>', indiv_DTR(rightFrame, empNumberToShow, \
beginDateToShow, endDateToShow, requiredReport)) # This is where i get the problem
def indiv_DTR(frame, empNumberToShow, beginDateToShow, endDateToShow, requiredReport):
dtr, absent, frequencyOfLate, frequencyOfUndertime, totalMinutesLate, totalMinutesUndertime, \
frequencyOfGracePeriod, gracePeriodTotal = initialization(empNumberToShow, beginDateToShow, endDateToShow, requiredReport)
tree = ttk.Treeview(frame, height=31)
tree['show'] = 'headings'
tree["columns"]=('Date', 'Day', 'AmIn', 'AM Out', 'PM In', 'PM Out', 'OT In', 'OT Out', 'Late', 'Early Out', 'Remarks')
tree.column('Date', width=60)
tree.column('Day', width=45 )
tree.column('AmIn', width=50)
tree.column('AM Out', width=50)
tree.column('PM In', width=50)
tree.column('PM Out', width=50)
tree.column('OT In', width=50)
tree.column('OT Out', width=50)
tree.column('Late', width=50)
tree.column('Early Out', width=65)
tree.column('Remarks', width = 95)
tree.heading('Date', text='Date')
tree.heading("Day", text="Day")
tree.heading("AmIn", text="AM In")
tree.heading('AM Out', text='AM Out')
tree.heading('PM In', text = 'PM In')
tree.heading('PM Out', text = 'PM Out')
tree.heading('OT In', text = 'OT In')
tree.heading('OT Out', text='OT Out')
tree.heading('Late', text='Late')
tree.heading('Early Out', text='Early Out')
tree.heading('Remarks', text='Remarks')
for i in dtr:
tree.insert("" , 'end', text= '', \
values=(i[0],i[1],i[2],i[3],i[4],i[5],i[6],i[7],i[8],i[9], i[10]))
tree.pack()
if __name__ == '__main__':
employee_toShow(leftFrame,rightFrame)
root.mainloop()
显然,这里只是一个新手(甚至不确定我的代码是否正确缩进)......任何帮助都会非常感激。
答案 0 :(得分:2)
当您为root.bind
致电<Return>
时,您将传递调用indiv_DTR(...)
的结果作为参数。这相当于以下内容:
res = indiv_DTR(rightFrame, empNumberToShow, beginDateToShow, endDateToShow, requiredReport)
root.bind('<Return>', res)
应该更清楚该函数已被执行。
要让绑定操作实际调用此函数,请传递方法名称。例如:
def onReturn(ev):
# call the indiv_DTR function
root.bind('<Return>', onReturn)
或者,如果需要将某些局部变量作为事件处理程序的参数捕获,则可以提供lambda表达式。
答案 1 :(得分:2)
请阅读有关发布minimal code的信息。
在这种情况下
root.bind('<Return>', indiv_DTR(rightFrame, empNumberToShow,
beginDateToShow, endDateToShow, requiredReport))
立即调用indiv_DTR,因为这就是你要说的!要延迟通话,您需要执行与Button调用相同的操作:使用lambda :
作为前缀。但是,要实际“执行Button”,这应该意味着调用它的回调,就像按下按钮一样,你应该使用.invoke方法。
root.bind('<Return>', emp_DTR.invoke)
(注意,不是emp_DTR.invoke()
)。这样,如果在Button调用中更改indiv_DTR参数,则不必记住在绑定调用中更改它们。
第三种选择是首先定义回调
def callback():
return indiv_DTR(rightFrame, empNumberToShow,
beginDateToShow, endDateToShow, requiredReport)
然后将callback
传递给Button和bind调用。我通常首先定义回调,即使我只将它们传递给一个widget构造函数,除非返回表达式非常短。