【问题标题】:How to take a Python function and create a simple Tkinter gui for it如何获取 Python 函数并为其创建一个简单的 Tkinter gui
【发布时间】:2012-12-11 01:17:48
【问题描述】:

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

CalculateStrenghtofBrute(Word, Charset) 计算字典(字) 计算密码短语(短语)

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

下面是一个示例函数

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

谢谢

【问题讨论】:

  • 请包含您的一些代码,如果没有看到它,很难想象您想要实现的目标
  • 您是在要求我们为您编写密码破解 GUI? “Tkinter 让我很难受”是什么意思?
  • 不,我只需要一个使用 Tkinter 从用户那里获取字符串并通过将结果字符串返回给 tkinter 的函数运行它的示例。我已经有了密码破解时间功能。
  • 我不确定在按下相应按钮时如何获取用户输入到我的函数的输入字段。此外,不知道如何在函数运行后将我的输出输出到 tkinter 小部件。
  • @user1376030:向我们展示您的尝试,我们可以指出如何让它变得更好。或者,花半天时间学习 Tkinter 教程。这应该是足够的时间来理解基础知识了。

标签: python-3.x tkinter


【解决方案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/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    相关资源
    最近更新 更多