【问题标题】:Tkinter 'Entry' object is not callableTkinter 'Entry' 对象不可调用
【发布时间】:2015-10-06 21:46:24
【问题描述】:

我创建的按钮出错。我的代码是这样的:

#!/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

【问题讨论】:

标签: python button tkinter


【解决方案1】:

在错误代码标识的行中,您有:

boton_ingresar=Button(..., entry_tipoGasto(),...)

注意您是如何尝试调用条目的(如错误消息所述),而不是在条目上调用 get 方法。

将代码更改为...entry_tipoGastro.get()...

【讨论】:

  • @matnagra:这是一个很好的例子,说明为什么应该避免 lambda:如果每个对 get() 的调用我们都在单独的行上,那么错误消息会做得更好查明错误。
  • 但是我如何制作一个不使用它的按钮呢?我找不到比使用 lambda 更好的例子,老实说,我不太喜欢那个命令......
  • @matnagra:由于您在全局命名空间中创建变量,因此它们随处可用。所以,只需在按钮调用的函数内调用entry_tipoGastro.get()
  • 你是对的......这实际上是一个更好的主意。谢谢!
【解决方案2】:

在您放置的按钮的构造函数中:

entry_tipoGasto()

而不是

entry_tipoGasto.get()

所以你尝试调用一个条目

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多