Python tkinter将函数绑定到按钮

Python tkinter将函数绑定到按钮,python,tkinter,Python,Tkinter,绑定到按钮的命令不会获得参数,因为事件的性质已经知道。删除“事件” 还可以将应答函数绑定到事件。结果是在不使用事件参数和使用事件参数的情况下都调用了答案。摆脱绑定调用 听从布莱恩的暗示。停止将数字字符串传递给。配置为位置参数。tk将尝试将is解释为字典。相反,将数字字符串添加到标签字符串的其余部分 与行一样,列从0开始 框架是不需要的 下面的修订工作 TypeError: answer() missing 1 required positional argument: 'event' 请公布实

绑定到按钮的命令不会获得参数,因为事件的性质已经知道。删除“事件”

  • 还可以将应答函数绑定到事件。结果是在不使用事件参数和使用事件参数的情况下都调用了答案。摆脱绑定调用

  • 听从布莱恩的暗示。停止将数字字符串传递给。配置为位置参数。tk将尝试将is解释为字典。相反,将数字字符串添加到标签字符串的其余部分

  • 与行一样,列从0开始

  • 框架是不需要的

  • 下面的修订工作

    TypeError: answer() missing 1 required positional argument: 'event'
    

    请公布实际错误。另外,您似乎给了一个数字作为
    ans.configure()
    的第一个参数-您希望它做什么?您好@BryanOakley这是一个错误(shell中没有显示任何内容)!我认为如果im更正,配置将更改标签。请不要从评论链接到图像。你有能力编辑你的答案。非常感谢@DanGetz被很好地发现非常感谢,我将尝试更多地了解我失败的地方。你是个英雄@特里·詹里迪
    TypeError: answer() missing 1 required positional argument: 'event'
    
    from tkinter import *
    
    root = Tk()
    root.title("Tip & Bill Calculator")
    
    totaltxt = Label(root, text="Total", font=("Helvitca", 16))
    tiptxt = Label(root, text="Tip (%)", font=("Helvitca", 16))
    peopletxt = Label(root, text="people", font=("Helvitca", 16))
    
    totaltxt.grid(row=0, column=0, sticky=E)
    tiptxt.grid(row=1, column=0, sticky=E)
    peopletxt.grid(row=2, column=0, sticky=E)
    
    totalentry = Entry(root)
    tipentry = Entry(root)
    peopleentry = Entry(root)
    
    totalentry.grid(row=0, column=1)
    tipentry.grid(row=1, column=1)
    peopleentry.grid(row=2, column=1)
    
    ans = Label(root, text = "ANS")
    ans.grid(row=4, column=0, columnspan=2, sticky=W)
    
    def answer():
        total = totalentry.get()
        tip = tipentry.get()
        people = peopleentry.get()
        if not (total and tip):
            ans['text'] = 'Enter total and tip as non-0 numbers'
        else:
            total = float(total)
            tip = float(tip) / 100
            people = int(people) if people else 1
            ans['text'] = str(round(total * tip / people, 2)) + " per person"
    
    calc = Button(root, text ="Calculate", fg = "black", command = answer)
    calc.grid(row=3, column=1)
    
    root.mainloop()