我输入了用于计算md5哈希值的代码,我希望在用户点击按钮时返回哈希值。但是,当我单击按钮时出现错误“TypeError:coercing to Unicode:need string or buffer,function found”。我尝试将哈希值转换为字符串,但它没有解决它。
def openfile():
filename = askopenfilename(parent=root)
f = open(filename)
f.read()
print (filename)
def hashmd5():
md5 = hashlib.md5()
with open(openfile, "rb") as f:
for block in iter(lambda: f.read(128), ""):
md5.update(block)
print (m.hexdigest)
def helloCallBack():
tkMessageBox.showinfo("Calculate MD5")
B = Button(text ="Calculate MD5", command = hashmd5)
B.pack()
更新:
def openfile():
filename = askopenfilename(parent=root)
f = open(filename)
f.read()
print (filename)
def savefile(self):
filename = tkFileDialog.asksaveasfilename(**self.file_opt)
if filename:
return open(filename, 'w')
def hashmd5():
md5 = hashlib.md5()
with (filename), "rb") as f:
for block in iter(lambda: openfile.read(128), ""):
md5.update(block)
print (md5.hexdigest)
答案 0 :(得分:1)
错误:“NameError:全局名称'文件名'未定义”是因为您没有在def hashmd5()函数中定义文件名,因此您需要使用self.filename才能使用文件名变量
答案 1 :(得分:0)
openfile
后缺少括号。但是,打开文件两次的重点是什么? (在openfile和hashmd5中都有?)