按钮对象在 tkinter 代码中不可调用 [英] Button object not callable in the tkinter code

查看:33
本文介绍了按钮对象在 tkinter 代码中不可调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from tkinter import * 
root=Tk()
#  not required here root.geometry()
root.title("calculator")
entry=Entry(root,width=50,borderwidth=5)
entry.grid(row=0,column=0,columnspan=3,padx=10,pady=10)
def button_add(number): 
    entry.insert(0,number)
def clear(): 
    entry.delete(0,END)
button_1=Button(root,text="1",padx=40,pady=20,command=lambda: button_add(1))
button_2=Button(root,text="2",padx=40,pady=20,command=lambda: button_add(2))
button_3=Button(root,text="3",padx=40,pady=20,command=lambda: button_add(3))
button_4=Button(root,text="4",padx=40,pady=20,command=lambda: button_add(4))
button_5=Button(root,text="5",padx=40,pady=20,command=lambda: button_add(5))
button_6=Button(root,text="6",padx=40,pady=20,command=lambda: button_add(6))
button_7=Button(root,text="7",padx=40,pady=20,command=lambda: button_add(7))
button_8=Button(root,text="8",padx=40,pady=20,command=lambda: button_add(8))
button_9=Button(root,text="9",padx=40,pady=20,command=lambda: button_add(9))
button_0=Button(root,text="0",padx=40,pady=20,command=lambda: button_add(0))
button_add=Button(root,text='+',padx=39,pady=20)
button_equal=Button(root,text='=',padx=95,pady=20)
def clear(): 
    entry.delete(0,END)
button_clear=Button(root,text='clear',padx=141.5,pady=10,command=clear)
# grid being set 
button_1.grid(row=1,column=0)
button_2.grid(row=1,column=1)
button_3.grid(row=1,column=2)
button_4.grid(row=2,column=0)
button_5.grid(row=2,column=1)
button_6.grid(row=2,column=2)
button_7.grid(row=3,column=0)
button_8.grid(row=3,column=1)
button_9.grid(row=3,column=2)
button_add.grid(row=4,column=0)
button_equal.grid(row=4,column=1,columnspan=2)
button_clear.grid(row=5, column=0,columnspan=4)

root.mainloop()

这是我的 tkinter 代码,我正在用 Python 创建一个计算器,如果我点击任何按钮,就会遇到这个错误

This is my tkinter code , i was creating a calculator in Python and I came across this error, if I clicked on any button

button_8=Button(root,text=8",padx=40,pady=20,command=lambda:button_add(8)) 类型错误:按钮"对象不可调用

button_8=Button(root,text="8",padx=40,pady=20,command=lambda: button_add(8)) TypeError: 'Button' object is not callable

请有人帮助我应该如何解决这个问题?

Please someone help how should I solve this ?

推荐答案

仔细看,你会发现你的函数和添加按钮变量的名字是一样的,因此python很困惑应该调用哪个,所以只需更改, 函数名称或按钮名称.

Take a closer look and you will notice that your function and add button variable have the same name, and hence python is confused which one to call, so just change, either the function name or the button name.

add_button = Button(root,text='+',padx=39,pady=20)
add_button.grid(row=4,column=0)

另外,我知道代码仍然不完整,但请记住这一点,每次单击数字按钮时,数字将显示在条目小部件的开头,以摆脱这种情况,例如:

Also, I know the code is still incomplete but just keep this in mind, each time you click on the number button, the number will be shown towards the starting of the entry widget, to get rid of this, say:

def button_add(number): 
    entry.insert(END,number) #END will always add the number to the end, rather than 0th postion like before

我现在告诉您,因为我认为您要避免这种情况的下一步是:

Im telling this now, because I think the next step your going to do to avoid this is:

def button_add(number):
    e = entry.get()
    entry.delete(0,END)
    entry.insert(0,e+str(number))

这完全没有必要,因为您在 tkinter 中有一个内置的 END 变量.

This is not necessary at all since you have a built in END variable with tkinter.

这篇关于按钮对象在 tkinter 代码中不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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