如何使用Python函数并为它创建一个简单的Tkinter gui

时间:2012-12-11 01:17:48

标签: python-3.x tkinter

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

CalculateStrenghtofBrute(Word,Charset) CalculateDictionary(字) CalculatePassPhrase(词组)

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

以下是样本函数

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+'.')

由于

1 个答案:

答案 0 :(得分: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/