2

我有三个函数 2 其中接受一个字符串并返回一个字符串。我有第三个函数,它接受两个字符串并返回一个字符串。我正在尝试创建一个简单的 Tkinter GUI,它将接收函数的任何参数,然后基于按钮按下运行我的算法返回结果。Tkinter 让我很难受。我需要四个输入字段来输入所有可能的参数,然后在按下按钮时运行正确的功能。函数将如下所示:

CalculateStrenghtofBrute(Word, Charset) CalculateDictionary(Word) CalculatePassPhrase(Phrase)

全部返回在函数中创建的字符串。

下面是一个示例函数

def wordTime(Password):

    with open('Dics/dict.txt','r') as f:
    Words = f.read().splitlines()

    found = Words.index(Password)
    found += 1
    timeSec = found*.1
    if(timeSec> 31536000):
        time = timeSec/31536000
        timeType = 'Years'
    elif(timeSec>86400):
        time = timeSec/86400
        timeType = 'Days'
    elif(timeSec>360):
        time = timeSec/360
        timeType = 'Hours'
    elif(timeSec>60):
        time = timeSec/60
        timeType = 'Minutes'
    else:
        time = timeSec
        timeType ='Seconds'


return ('Cracking',Password,'using dictionary attack will take', round(time, 2), timeType+'.')

谢谢

4

1 回答 1

1

如果你想从用户那里获取输入,你需要创建一个输入框,一旦你有了一个输入框,你可以调用它的 get 方法来获取当前驻留在输入框中的字符串,我已经使用了你的示例函数并为它制作了一个简单的 tk GUI:

import Tkinter as tk

def wordTime():
    password = input_box.get()
    with open('Dics/dict.txt','r') as f:
        Words = f.read().splitlines()
        found = Words.index(Password)
        found += 1
        timeSec = found*.1
        if(timeSec> 31536000):
            time = timeSec/31536000
            timeType = 'Years'
        elif(timeSec>86400):
            time = timeSec/86400
            timeType = 'Days'
        elif(timeSec>360):
            time = timeSec/360
            timeType = 'Hours'
        elif(timeSec>60):
            time = timeSec/60
            timeType = 'Minutes'
        else:
            time = timeSec
            timeType ='Seconds'
    print ('Cracking',Password,'using dictionary attack will take', round(time, 2), timeType+'.')

# Make a top level Tk window
root = tk.Tk()
root.title("Cracker GUI v.01")
# Set up a Label
grovey_label = tk.Label(text="Enter password:")
grovey_label.pack(side=tk.LEFT,padx=10,pady=10)
# Make an input box
input_box = tk.Entry(root,width=10)
input_box.pack(side=tk.LEFT,padx=10,pady=10)
# Make a button which takes wordTime as command,
# Note that we are not using wordTime()
mega_button = tk.Button(root, text="GO!", command=wordTime)
mega_button.pack(side=tk.LEFT)
#Lets get the show on the road
root.mainloop()

如果你想获取多个值,你可以使用多个按钮来设置多个变量,我也不确定你的功能,但这并不是真正的问题。

作为参考,以下站点有一些很好的基本示例:

http://effbot.org/tkinterbook/entry.htm

http://effbot.org/tkinterbook/button.htm

http://www.ittc.ku.edu/~niehaus/classes/448-s04/448-standard/simple_gui_examples/

于 2013-05-19T11:18:34.807 回答