Button-Objekt im tkinter-Code nicht aufrufbar
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()
Dies ist mein tkinter-Code. Ich habe einen Taschenrechner in Python erstellt und bin auf diesen Fehler gestoßen, wenn ich auf eine Schaltfläche geklickt habe
button_8 = Button (root, text = "8", padx = 40, pady = 20, command = lambda: button_add (8)) TypeError: Das 'Button'-Objekt kann nicht aufgerufen werden
Bitte jemand helfen, wie soll ich das lösen?
Antworten
Wenn Sie genauer hinschauen, werden Sie feststellen, dass Ihre Funktion und die Variable zum Hinzufügen von Schaltflächen denselben Namen haben. Daher ist Python verwirrt, welche aufgerufen werden soll. Ändern Sie also entweder den Funktionsnamen oder den Schaltflächennamen.
add_button = Button(root,text='+',padx=39,pady=20)
add_button.grid(row=4,column=0)
Ich weiß auch, dass der Code immer noch unvollständig ist, aber denken Sie daran, dass jedes Mal, wenn Sie auf den Nummernknopf klicken, die Nummer am Anfang des Eintrags-Widgets angezeigt wird, um dies zu beseitigen, sagen wir:
def button_add(number):
entry.insert(END,number) #END will always add the number to the end, rather than 0th postion like before
Ich erzähle das jetzt, weil ich denke, der nächste Schritt, den Sie tun werden, um dies zu vermeiden, ist:
def button_add(number):
e = entry.get()
entry.delete(0,END)
entry.insert(0,e+str(number))
Dies ist überhaupt nicht erforderlich, da Sie ENDmit tkinter eine integrierte Variable haben.