Tkinter标签输出错误

2024-04-26 11:56:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了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

Tags: 代码目的部件tkinter标签filenameusersdirectory
1条回答
网友
1楼 · 发布于 2024-04-26 11:56:49

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

(显然完全没有经过测试)

相关问题 更多 >