繁体   English   中英

带有gui控制台的Python pyqt文件哈希器

[英]Python pyqt file hasher with gui console

我正在尝试在gui中制作一个控制台,例如文本框,并告诉我文件夹中是否有使用相同md5哈希值的图片。

我真的很困惑为什么这对我不起作用。 我已经尝试了许多不同的方法来执行此操作,但对我来说没有任何效果。

这是我正在使用的代码(当您玩得开心时请注意,它不会给您任何错误,但不起作用)。

import webbrowser, hashlib, os, sys, time, random, win32api, re , time, subprocess
from PyQt4.QtCore import QSize, QTimer, QRect, pyqtSlot
from PyQt4.QtGui import QApplication,QLineEdit ,QGraphicsRectItem , QMainWindow, QPushButton, QWidget, QIcon, QLabel, QPainter, QPixmap, QMessageBox, QAction, QKeySequence, QFont, QFontMetrics, QMovie
from PyQt4 import QtGui

class UIWindow(QWidget):
    def __init__(self, QWidget, parent=None):
        super(UIWindow, self).__init__(parent)
        self.resize(QSize(400, 450))

        self.textbox = QLineEdit('dance',QWidget)
        self.textbox.move(20, 300)
        self.textbox.resize(280,300)

        self.btn = QPushButton('files',self)
        self.btn .resize(100, 40)
        self.btn .move(260, 400)

        def sesh():
            for root, dirs,files in os.walk("C:\Users\Matt\Desktop\photos", topdown=True):
                for name in files:
                    #print(os.path.join(root, name))
                    FileName = (os.path.join(root, name))
                    hasher = hashlib.md5()
                    with open(str(FileName), 'rb') as afile:
                        buf = afile.read()
                        hasher.update(buf)
                    if (hasher.hexdigest()) == '653cd1d521d8f343c998e0d568a1e5ea':
                        self.textbox.setText('file is here')
                    if (hasher.hexdigest()) == 'd41d8cd98f00b204e9800998ecf8427e':
                        self.textbox.setText('file is here')
                    if (hasher.hexdigest()) == '03c7c0ace395d80182db07ae2c30f034':
                        self.textbox.setText('file is here')
                    if (hasher.hexdigest()) == '6c0cbf5029aed0af395ac4b864c6b095':
                        self.textbox.setText('file is here')
                    else:
                        self.textbox.setText ("file is NOT here")
        def click():
            self.textbox.setText('Button clicked.' +str(sesh()))

        self.btn .clicked.connect(click)

class MainWindow(QMainWindow,):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setGeometry(50, 50, 1000, 1000)
        self.setFixedSize(950, 620)
        self.startUIWindow()
        self.setWindowIcon(QtGui.QIcon('Images\Logo.png'))

    def startUIWindow(self):
        self.Window = UIWindow(self)
        self.setWindowTitle("pythonw")
        self.setCentralWidget(self.Window)
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindow()
    sys.exit(app.exec_())

如果有人可以为我完成这项工作,那将是惊人的,我将非常感激,我现在​​完全迷失了。

您没有从函数sesh()返回任何内容。

您正在sesh()中设置文本,然后立即在click()中覆盖它。

更改这些行:

self.textbox.setText('file is here')

至:

return 'file is here'

(或“不在此处”),您将得到答案。

注意:可能只是网页格式,但是btn后面似乎有空格:

self.btn .clicked.connect(click)

编辑:

为了使输出更具描述性,请更改以下部分:

if (hasher.hexdigest()) == '653cd1d521d8f343c998e0d568a1e5ea':
    self.textbox.setText('file is here')
if (hasher.hexdigest()) == 'd41d8cd98f00b204e9800998ecf8427e':
    self.textbox.setText('file is here')
if (hasher.hexdigest()) == '03c7c0ace395d80182db07ae2c30f034':
    self.textbox.setText('file is here')
if (hasher.hexdigest()) == '6c0cbf5029aed0af395ac4b864c6b095':
    self.textbox.setText('file is here')
else:
    self.textbox.setText ("file is NOT here")

到:

output = ''
multi_files = False
if (hasher.hexdigest()) == '653cd1d521d8f343c998e0d568a1e5ea':
    output += 'file1'
    multi_files = True
if (hasher.hexdigest()) == 'd41d8cd98f00b204e9800998ecf8427e':
    if multi_files == True:
        output += ', file2'
    else:
        output += 'file2'
        multi_files = True
if (hasher.hexdigest()) == '03c7c0ace395d80182db07ae2c30f034':
    if multi_files == True:
        output += ', file3'
    else:
        output += 'file3'
        multi_files = True
if (hasher.hexdigest()) == '6c0cbf5029aed0af395ac4b864c6b095':
    if multi_files == True:
        output += ', file4'
    else:
        output += 'file4'
        multi_files = True
output += ' found'
if multi_files == False:
    output("no files here")
return output

并更改此行:

self.textbox.setText('Button clicked.' +str(sesh()))

至:

self.textbox.setText(str(sesh()))

附加说明:如果您确实想要多行,则不能使用QLineEdit。 如果仅输出文本(看起来确实如此),请使用QLabel,它可以是多行。 您在需要换行的字符串中添加“ \\ n”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM