TypeError:使用Tkinter获取正好1个参数(给定0)

时间:2017-10-05 06:15:40

标签: python tkinter typeerror

我正在使用复选框列表编写Tkinter代码。

当我尝试执行代码时,GUI工作正常,直到我点击“触发”按钮。

这是我的代码:

from Tkinter import*

root = Tk()
root.wm_title("Triggering System")
root.config(background = "#2471A3")

def terminate():
    global root
    root.destroy()


def var_states(var1):
    file_name = r'D:\log.txt'
    with open(file_name, 'wb') as x_file:
        x_file.write('{} TotalAmount'.format(var1()))


leftFrame = Frame(root, width=800, height=600)
leftFrame.grid(row=0 , column=0, padx=10, pady=4)
leftFrame.config(background = "#D6EAF8")
leftFrame.pack(fill=X)

firstLabel = Label(leftFrame, text="Triggering System")
firstLabel.grid(row=1, column=0, padx=10, pady=2)
firstLabel.config(background = "#2471A3")
firstLabel.config(font=("Impact", 22))
firstLabel.pack(fill=X)

secondLabel = Label(leftFrame, text="Downtime Cause:",anchor = "w")
secondLabel.grid(row=2, column=0, padx=10, pady=2)
secondLabel.config(background = "#D6EAF8")
secondLabel.config(font=("Arial Narrow", 18))
secondLabel.pack(fill=X)

var1 = IntVar()
var1 = Checkbutton(leftFrame, text="Material Shortages",variable=var1,anchor = "w")
var1.grid(row=3, column=0, padx=10, pady=2)
var1.config(background = "#F7DC6F")
var1.config(font=("Tahoma", 12))
var1.pack(fill=X)

var2 = IntVar()
var2 = Checkbutton(leftFrame, text="Wrong Component",variable=var2,anchor = "w")
var2.grid(row=4, column=0, padx=10, pady=2)
var2.config(background = "#F7DC6F")
var2.pack(fill=X)
var2.config(font=("Tahoma", 12))

SendResult = Button(leftFrame, text = "Trigger", command=var_states)
SendResult.grid(row=5, column=1, padx=2, pady=2)
SendResult.config(font=("Tahoma", 12))
SendResult.pack(fill=X)


mainloop()

然而,点击“Triger”按钮后,我收到了这样的错误:

Exception in Tkinter callback
    Traceback (most recent call last):
      File "C:\Python27\lib\lib-tk\Tkinter.py", line 1542, in __call__
        return self.func(*args)
    TypeError: var_states() takes exactly 1 argument (0 given)

1 个答案:

答案 0 :(得分:0)

您将方法定义为采用一个参数

def var_states(var1):

但是你没有任何调用它。

SendResult = Button(..., command=var_states)

您可以在此处使用lambda将参数传递给您的方法。

SendResult = Button(...,  command= lambda: var_states(argumentToSend))