Python打印列表对象[0x01FEAD50处的tkinter.StringVar对象],但不是列表中的项目* Tkinter *

时间:2016-05-18 17:20:33

标签: python list user-interface tkinter

我正在尝试创建一个能够使用MD5哈希监控文件完整性的GUI(更新日志的实际监控可以在命令提示符下)。

我让初始命令行程序完美运行,但在使用tkinter将其转换为基于GUI的版本时出现问题。

我使用GUI创建了一个我希望在“addFiles”函数中监控的文件列表,但是当我尝试将该列表传递给“checkForIntegrity”函数(或打印)时我在该函数中使用我的测试print(listOfFiles)代码的列表),我得到的只是[tkinter.StringVar object at 0x01FEAD50],但是没有得到实际的列表。

我已经广泛搜索了一个答案,并尝试在不同的位置使用“listOfFiles.get()”的各种实现,但没有成功。

我不知道为什么我只获得实际的列表对象但没有列出的项目,我的代码如下。

提前感谢大家。

编辑:为了清楚起见,我的'GUI()'函数创建了一个窗口,询问用户想要监视多少文件并将其传递给'addFiles()'函数,该函数允许输入文件数量他们指明。我需要能够通过列表将他们在GUI中指定的文件传递给程序。再次感谢。

import hashlib
import time
from tkinter import *

def main():  
    GUI()

def GUI():
    window = Tk()
    window.title("Integrity Checker")
    frame1 = Frame(window)
    frame1.pack()
    label1 = Label(frame1, text = "***Proof Of Concept Program That Monitors the Integriry of Files***")
    label1.grid(row = 1, column = 1)

    frame2 = Frame(window)
    frame2.pack()
    getNumberOfFiles = Label(frame2, text = "Insert Number of Files You Would Like to Check: ")
    getNumberOfFiles.grid(row = 2, column = 1)
    numberOfFiles = IntVar()
    NumberOfFilesOption = Entry(frame2, textvariable = numberOfFiles)
    NumberOfFilesOption.grid(row = 2, column = 2)
    button = Button(frame2, text = "OK", command = lambda : addFiles(numberOfFiles))
    button.grid(row = 2, column = 3)


    window.mainloop()


def addFiles(numberOfFiles):

    listOfFiles = []    

    window = Tk()
    window.title("Add Files")
    frame1 = Frame(window)
    frame1.pack()
    label1 = Label(frame1, text = "***Select The Files You Want To Monitor***")
    label1.grid(row = 1, column = 1)


    for i in range (numberOfFiles.get()):
        AddFile = Label(frame1, text = "Add File:")
        AddFile.grid(row = (i + 3), column = 1)
        FileName = StringVar()
        FileNameOption = Entry(frame1, textvariable = FileName)
        FileNameOption.grid(row = (i + 3), column = 2)
        button = Button(frame1, text = "OK", command = lambda : listOfFiles.append(FileName))
        button.grid(row = (i + 3), column = 3)
    button2 = Button(frame1, text = "Done", command = lambda : checkforINTEGRITY(numberOfFiles, listOfFiles))
    button2.grid(row = (i + 4), column = 2)

    window.mainloop()


def checkforINTEGRITY(numberOfFiles, listOfFiles):

    #Number = numberOfFiles.get()
    #listOfFiles = []
    #count = 0
    #numberOfFiles = eval(input("How many files would you like to monitor?: "))
    #while count < Number:
     #   filename = input("Enter the name of the file you would like to check: ")
     #   count += 1
     #   listOfFiles.append(filename)

    print(listOfFiles)
    i = 0
    originalList = []
    for file in listOfFiles:
        original_md5 = hashlib.md5(open(listOfFiles[i],'rb').read()).hexdigest()
        originalList.append(original_md5)        
        i += 1
    print(originalList)

    while True:

        i = 0
        while i < Number:
            md5_returned = hashlib.md5(open(listOfFiles[i],'rb').read()).hexdigest()

            print(md5_returned)

            if originalList[i] == md5_returned:
                print("The file", listOfFiles[i], "has not changed")
            else:
                print("The file", listOfFiles[i], "has been modified!")
            i += 1

        time.sleep(5)

main()

1 个答案:

答案 0 :(得分:0)

您似乎想在get上调用FileNameOption方法:

button = Button(frame1, text = "OK", command = lambda : listOfFiles.append(FileNameOption.get()))

通过此更改,我可以在listOfFiles中获取字符串列表。