Tkinter标签输出错误

时间:2018-01-19 20:11:16

标签: python tkinter string-formatting

我编写了python代码,目的是在tkinter.Label窗口小部件中输出MD5哈希值,但我在标签中的输出如下:

{filename: } llos.docx{  MD5:}312b1face983fe7ad82bd9909888680

然而,我希望输出显示在我的控制台中:

    Directory: C:\Users\User\Documents\Test
Filename:  cpck2.py      MD5:  1d05f6e9c551280098987dd32b64a2d
Filename:  llos.docx     MD5:  312b1face97877ad82bd87e4fa78680

1 个答案:

答案 0 :(得分:0)

print()函数允许您连接用逗号分隔的对象。这是唯一的地方,你可以这样做。在所有其他地方,您需要自己进行字符串格式化。

centerLabel['text'] = 'Filename: {}\t MD5:{}'.format(x, hashupdate.hexdigest())

编辑:或者如果你想要所有这些:

rootDir = r"C:\Users\Ghost\Documents\MalwareTest"
output = []
for dirName, subdirList, fileList in os.walk(rootDir, topdown=True):
    print('Directory:', dirName)
    for x in fileList:
        hashupdate = hashlib.md5()   
        with open(os.path.join(dirName, x), 'rb') as f:
            hashupdate.update(f.read())
        output.append('Filename: {}\t MD5:{}'.format(x, hashupdate.hexdigest()))
output = '\n'.join(output)
print(output)

centerLabel = Label(root, bg='white', width=65, height=13, padx=3, pady=3, anchor=NW,relief=SUNKEN)
centerLabel['text'] = output

(显然完全未经测试)

相关问题