Python 特金特';条目';对象不可调用
我创建的按钮出现错误。我的代码是:Python 特金特';条目';对象不可调用,python,button,tkinter,Python,Button,Tkinter,我创建的按钮出现错误。我的代码是: #!/usr/bin/python from tkinter import * root = Tk() root.resizable(width=FALSE, height=FALSE) #funcion para agregar datos nuevos def agregarDato(fecha,campo,labor,tipoGasto,monto,loggedBy,detalle): dato=[fecha,campo,labor,tipoG
#!/usr/bin/python
from tkinter import *
root = Tk()
root.resizable(width=FALSE, height=FALSE)
#funcion para agregar datos nuevos
def agregarDato(fecha,campo,labor,tipoGasto,monto,loggedBy,detalle):
dato=[fecha,campo,labor,tipoGasto,monto,loggedBy,detalle]
mesAno=fecha.split('/')[1]+'-'+fecha.split('/')[2]
#si el archivo mes-ano existe entonces se edita. Si no, se crea y agrega
archivo=open(mesAno+'.txt', 'a')
archivo.write(str(dato)+'\n')
leftFrame=Frame(root, width=500,height=300)
leftFrame.pack(side=LEFT)
rightFrame=Frame(root, width=500,height=300)
rightFrame.pack()
### Texto y cajas
label_fecha=Label(leftFrame,text='Fecha:')
label_campo=Label(leftFrame,text='Campo:')
label_labor=Label(leftFrame,text='Labor:')
label_tipoGasto=Label(rightFrame,text='Tipo de gasto:')
label_monto=Label(rightFrame,text='Monto:')
label_detalle=Label(rightFrame,text='Detalle:')
label_loggedBy=Label(leftFrame,text='Autor:')
entry_fecha=Entry(leftFrame)
entry_campo=Entry(leftFrame)
entry_labor=Entry(leftFrame)
entry_tipoGasto=Entry(rightFrame)
entry_monto=Entry(rightFrame)
entry_detalle=Entry(rightFrame)
entry_loggedBy=Entry(leftFrame)
label_fecha.grid(row=0,column=0,sticky=E)
label_campo.grid(row=1,column=0,sticky=E)
label_labor.grid(row=2,column=0,sticky=E)
label_tipoGasto.grid(row=0,column=0,sticky=E)
label_monto.grid(row=1,column=0,sticky=E)
label_detalle.grid(row=2,column=0,sticky=E)
label_loggedBy.grid(row=3,column=0,sticky=E)
entry_fecha.grid(row=0,column=1)
entry_campo.grid(row=1,column=1)
entry_labor.grid(row=2,column=1)
entry_tipoGasto.grid(row=0,column=1)
entry_monto.grid(row=1,column=1)
entry_detalle.grid(row=2,column=1)
entry_loggedBy.grid(row=3,column=1)
###botones ingresar y volver
boton_ingresar=Button(rightFrame,text='Ingresar',command= lambda: agregarDato(entry_fecha.get(),entry_campo.get,entry_labor.get(),entry_tipoGasto(),entry_monto.get(),entry_loggedBy.get(),entry_detalle.get()))
boton_ingresar.grid(row=3,column=0)
root.mainloop()
我得到的错误是:
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1487, in __call__
return self.func(*args)
File "C:\Users\Matias\Desktop\Proyecto MiPi\Int_pagAgregarDato.py", line 56, in <lambda>
boton_ingresar=Button(rightFrame,text='Ingresar',command= lambda: agregarDato(entry_fecha.get(),entry_campo.get(),entry_labor.get(),entry_tipoGasto(),entry_monto.get(),entry_loggedBy.get(),entry_detalle.get()))
TypeError: 'Entry' object is not callable
回溯(最近一次呼叫最后一次):
文件“C:\Python34\lib\tkinter\\ uuuuu init\uuuuuu.py”,第1487行,在调用中__
返回self.func(*args)
文件“C:\Users\Matias\Desktop\Proyecto MiPi\Int_pagAgregarDato.py”,第56行,在
boton_Ingregar=按钮(右框,text='Ingrear',command=lambda:agregarDato(entry_fecha.get(),entry_campo.get(),entry_labor.get(),entry_tipoGasto(),entry_monto.get(),entry_loggedBy.get(),entry_detalle.get())
TypeError:“Entry”对象不可调用
在由错误代码标识的行中,您有:
boton_ingresar=Button(..., entry_tipoGasto(),...)
请注意,您是如何尝试调用条目的(如错误消息所述),而不是调用条目上的get
方法的
将按钮的构造函数中的代码更改为
…entry\u tipoGastro.get()…
:
entry_tipoGasto()
而不是
entry_tipoGasto.get()
因此,您尝试调用一个条目您可以将其缩减为一个吗?@matnagra:这是一个很好的例子,说明了为什么应该避免
lambda
:如果对get()的每个调用
我们在一条单独的线路上,错误消息可以更好地定位错误。但是我如何制作一个按钮,不使用它就能完成相同的任务呢?我找不到比使用lambda更好的例子,老实说,我真的不喜欢这个命令…@matnagra:因为您在全局名称空间中创建变量,所以它们在任何地方都可用。因此,只需在按钮调用的函数中调用entry\u tipoGastro.get()
。您是对的。。。这实际上是个更好的主意。谢谢