2017-02-24 60 views
0

我想在这里gui使一个控制台像文本框,并告诉我,如果我有我的文件夹中使用相同的MD5哈希图片。Python的pyqt文件哈希与gui控制台

我真的很困惑,为什么这不适合我。我已经尝试了很多不同的方式来做这件事,而且没有任何事情可以为我工作。

这是我正在使用的代码(当你玩的时候记住你没有错误但不起作用)。

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

,如果有人能做出这样的工作对我来说这将是惊人的,我会非常感激我只是完全在这一点上失去了。

回答

1

您没有从函数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'。

+0

这并没有解决我的问题,谢谢试图帮助,虽然我仍然没有得到任何东西 – Tyrell

+0

哦,我调整了一件事,现在做的工作有一种方法,你会知道如何打印更多,然后在我的QLineEdit一行,因为一的哈希不在我的文件夹中,它的正义告诉我,其中一个文件在那里,但没有告诉我,其中一个文件不在文件夹 – Tyrell

+0

(1)中,请说明你的调整是完成它 - (2)最简单的方法不是试图让它多行,而是改变你的返回值,使其更具描述性。我将编辑我的答案以提供示例。 – Alan