Clint
Clint

Reputation: 93

How to take a Python function and create a simple Tkinter gui for it

I have three functions 2 of which take a string and return a string. I have a third function that takes two strings and returns a string. I am trying to create a simple Tkinter GUI that would take in any parameters of the functions then based on the button press run my algorithm returning the result. Tkinter is giving me a hard time. I need four input fields for all possible parameters then run the correct function on press of button. Functions will look like:

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

All return a string created within the functions.

Below is a Sample Function

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

Thanks

Upvotes: 2

Views: 2664

Answers (1)

Noelkd
Noelkd

Reputation: 7906

If you want to take input from the user you need to create an entry box, once you have an entry box you can call the get method on it to get the string which currently resides in the entry box, I've took your example function and made a simple tk GUI for it:

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

If you wanted to take multiple values you could use multiple buttons which set multiple variables, also I'm not sure about your function but that's not really the question at hand.

For reference the following sites have some good basic examples:

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/

Upvotes: 1

Related Questions