简体   繁体   中英

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.

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

You are setting the text in sesh() and then immediately overwriting it in 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:

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. If you are only outputting text (which it seems you are) the use a QLabel, which can be multi-line. You add '\\n' to your string where you need the line break.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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