小编典典

Python Tkinter将功能绑定到按钮

python

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, sticky=E)
tiptxt.grid(row=1, sticky=E)
peopletxt.grid(row=2, sticky=E)

totalentry = Entry(root)
tipentry = Entry(root)
peopleentry = Entry(root)

totalentry.grid(row=0, column=2)
tipentry.grid(row=1, column=2)
peopleentry.grid(row=2, column=2)

ans = Label(root, text = "ANS")
ans.grid(row=4)

def answer(event):
    data1 = totalentry.get()
    data2 = tipentry.get()
    data3 = peopleentry.get()
    if tipentry.get() == 0:
        ans.configure(str((data1/data3)), text="per person")
        return
    elif data1 == 0:
        ans.configure(text="Specify the total")
        return
    elif data3 == 0 or data3 ==1:
        ans.configure(str(data1*(data2/100+1)))
        return
    elif data1 == 0 and data2 == 0 and data3 ==0:
        ans.configure(text = "Specify the values")
        return
    else:
        ans.configure(str((data1*(data2/100+1)/data3)), text="per person")
        return

bf = Frame(root)
bf.grid(row=3, columnspan=3)
calc = Button(bf, text ="Calculate", fg = "black", command = answer)
calc.bind("<Button-1>", answer)
calc.grid(row=3, column=2)

root.mainloop()

我正在尝试制作一个简单的小费和账单计算器,以供学习和实验。但是,我遇到了一个可怕的问题,困扰了好几天,我通常在python中使用函数苦苦挣扎,而且我试图将一个函数绑定到一个calculate按钮,使它得以显示。但是,我无法使其正常工作。经过一番混乱后,当我单击“计算”按钮时,我以这个错误结束。

单击计算按钮后,这是错误:

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

阅读 217

收藏
2021-01-20

共1个答案

小编典典

  1. 绑定到按钮的命令不会获取参数,因为事件的性质是已知的。删除“事件”。

  2. 您还将答案功能绑定到事件。结果是,无论是否带有事件参数都将调用答案。摆脱绑定调用。

  3. 遵循Bryan给出的提示。停止将数字字符串传递给.configure作为位置参数。tk会尝试将其解释为字典。而是将数字字符串添加到标签字符串的其余部分。

  4. 像行一样,列从0开始。

  5. 不需要框架。

以下修订适用。

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()
2021-01-20