Python tkinter 将函数绑定到按钮 [英] Python tkinter binding a function to a button

查看:66
本文介绍了Python tkinter 将函数绑定到按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 中与函数斗争,我试图将一个函数绑定到一个计算按钮,我设法让它出现.但是,我无法让它发挥作用.当我点击计算按钮时,经过一些混乱,我以这个错误结束.

I'm trying to make a tip and bill calculator with a simple design just to learn and experiment. However, I encountered a horrible problem that kept haunting for days, I usually struggle with functions in python and I'm trying to bind a function to a calculate button, which I managed to make it appear. However, I can't manage to get it to work. After some messing around I ended with this error, when I click the calculate button.

这是我点击计算按钮后的错误:

This is the error after I click the calculate button:

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

推荐答案

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

  1. Commands bound to a button do not get an argument, as the nature of the event is already known. Delete 'event'.

您还将应答函数绑定到一个事件.结果是在没有和有事件参数的情况下调用 answer.摆脱绑定调用.

You also bind the answer function to an event. The result is that answer is called both without and with an event argument. Get rid of the bind call.

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

Follow hint given by Bryan. Stop passing a digit string to .configure as a positional parameter. tk will try to interpret is as dictionary. Instead, add the number string to the rest of the label string.

像行一样,列从 0 开始.

Like rows, columns start from 0.

不需要框架.

以下修订有效.

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()

这篇关于Python tkinter 将函数绑定到按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆