簡體   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