python - 绑定(bind)到 Enter 的 Tkinter 按钮立即执行

标签 python python-2.7 tkinter

<分区>

这些问题的答案:How to pass arguments to a Button command in Tkinter?Why is Button parameter “command” executed when declared?非常感谢。

但是,当我将按钮绑定(bind)为按下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()

显然,这里只是一个新手(甚至不确定我的代码是否缩进正确)......非常感谢任何帮助。

最佳答案

当您调用 root.bind 时对于 <Return>您正在传递调用 indiv_DTR(...) 的结果作为参数。这等效于以下内容:

res = indiv_DTR(rightFrame, empNumberToShow, beginDateToShow, endDateToShow, requiredReport)
root.bind('<Return>', res)

这应该更清楚地表明函数已经执行。

要让绑定(bind)操作实际调用此函数,您需要传递方法名称。例如:

def onReturn(ev):
    # call the indiv_DTR function

root.bind('<Return>', onReturn)

或者,如果您需要捕获一些局部变量作为事件处理程序的参数,您可以提供一个 lambda 表达式。

关于python - 绑定(bind)到 Enter 的 Tkinter 按钮立即执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27770219/

相关文章:

python - 在python中并行执行任务

python - 使用 Python 请求发送 SOAP 请求

python - 检查 None、False、True

python - 可以处理语法错误吗?

python - 如何将滚动条附加到文本小部件?

python - 如何在 tkinter 窗口中显示视频预览

python - 如何从 df.groupby 绘制堆积条形图 ('feature' ) ['label' ].value_counts()

python - 替换大于 Pandas 数据框中数字的值

python - 保持相同的SyntaxError : invalid syntax when trying to load packages using pypm

python - 按位置删除文本 tkinter