简体   繁体   English

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

[英]Python pyqt file hasher with gui console

I am trying to make a console like text box in a gui here and have it tell me if I have a picture in my folder that uses the same md5 hash. 我正在尝试在gui中制作一个控制台,例如文本框,并告诉我文件夹中是否有使用相同md5哈希值的图片。

I'm really confused on why this is not working for me. 我真的很困惑为什么这对我不起作用。 I have tried so many different ways in doing this and nothing is working for me. 我已经尝试了许多不同的方法来执行此操作,但对我来说没有任何效果。

Here is my code I'm working with (mind you when you fun it it gives you no errors but doesn't work). 这是我正在使用的代码(当您玩得开心时请注意,它不会给您任何错误,但不起作用)。

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_())

if someone could make this work for me that would be amazing and i would be so grateful i am just completely lost at this point. 如果有人可以为我完成这项工作,那将是惊人的,我将非常感激,我现在​​完全迷失了。

You are not returning anything from function sesh(). 您没有从函数sesh()返回任何内容。

You are setting the text in sesh() and then immediately overwriting it in click(). 您正在sesh()中设置文本,然后立即在click()中覆盖它。

Change these lines: 更改这些行:

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

to: 至:

return 'file is here'

(or 'not here' as appropriate) and you will have your answer. (或“不在此处”),您将得到答案。

Note: might just be the webpage formatting but you seem to have a space after the btn: 注意:可能只是网页格式,但是btn后面似乎有空格:

self.btn .clicked.connect(click)

EDIT: 编辑:

To make the output more descriptive, change this part: 为了使输出更具描述性,请更改以下部分:

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")

over to: 到:

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

and change this line: 并更改此行:

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

to: 至:

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

Additional comment: if you really want multi-line, you can't use a QLineEdit. 附加说明:如果您确实想要多行,则不能使用QLineEdit。 If you are only outputting text (which it seems you are) the use a QLabel, which can be multi-line. 如果仅输出文本(看起来确实如此),请使用QLabel,它可以是多行。 You add '\\n' to your string where you need the line break. 您在需要换行的字符串中添加“ \\ n”。

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

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