如何从菜单中弹出消息框?

时间:2017-01-05 01:00:40

标签: python tkinter messagebox

我是Python的新手。我想用Tkinter创建一个程序,该程序从条目"框"中获取条目。然后将它的每个字符与字符集进行比较,最后弹出一个显示该短语的消息框。我几乎完成了它,但是我无法使这条线工作:

info_menu.add_command(label="About",
                      command=messagebox.showwarning(message="Creator: GakPower\nVersion: 1.0.0\nCreated at 1/5/2017")

完整代码:

from tkinter import ttk
from tkinter import *
from tkinter import messagebox
import string as s


class Bruteforcer:

    def output(self, result):
        self.result = result
        if result == "":
            messagebox.showwarning(title="Enter a Phrase",
                                   message="Please enter a Phrase!")
        else:
            messagebox.showinfo(title="Phrase Found!",
                                message=("The process completed Successfully!\n The Phrase Found!!!!\n", result))

    def submit_button_pressed(self):
        entry_val = self.entry_value.get()
        charset = list(s.ascii_letters + "ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩαβγδεζηθικλμνξοπρστυφχψω" + s.digits + s.punctuation)
        result = ""
        x = 0
        while x <= len(entry_val)-1:
            echar = entry_val[x]
            for char in charset:
                if char == echar:
                    result += echar
                    x += 1
                    break
        return self.output(result)

    def __init__(self, root):

        self.entry_value = StringVar(root, "")

        self.the_menu = Menu(root)
        info_menu = Menu(self.the_menu, tearoff=0)
        **info_menu.add_command(label="About",
                              command=messagebox.showwarning(message="Creator: GakPower\nVersion: 1.0.0\nCreated at 1/5/2017")**
                              )
        info_menu.add_separator()
        info_menu.add_command(label="Quit", command=root.quit)
        self.the_menu.add_cascade(label="Info", menu=info_menu)
        root.config(menu=self.the_menu)

        text_fond = StringVar()
        text_fond.set("Times")

        root.title("Graphical Bruteforcer")
        root.geometry("500x500")
        root.resizable(width=False, height=False)
        style = ttk.Style()
        style.configure("TButton",
                        foreground="red",
                        fond="Times 20",
                        padding=10)

        style.configure("TEntry",
                        foreground="red",
                        fond="Times 20",
                        padding=10)
        style.configure("TLabel",
                        foreground="red",
                        fond="Times 35 Bold")
        # ---------- Entry -----------
        self.entry_value = ttk.Entry(root,
                                     textvariable=self.entry_value, width=25, state="normal")
        self.entry_value.grid(row=1, columnspan=2)
        # ---------- Label ----------
        self.secret_label = ttk.Label(root,
                                      text="The Secret Phrase").grid(row=0, column=0)
        # ---------- Button ---------
        self.button_submit = ttk.Button(root,
                                        text="Submit", command=self.submit_button_pressed)
        self.button_submit.grid(row=1, column=2)
root = Tk()

brute = Bruteforcer(root)

root.mainloop()

1 个答案:

答案 0 :(得分:0)

正如furas在评论中所说,command=期望一个功能。所以你应该替换

info_menu.add_command(label="About",
                      command=messagebox.showwarning(message="Creator: GakPower\nVersion: 1.0.0\nCreated at 1/5/2017"))

类似

def show_about():
    ''' show the about messagebox '''
    messagebox.showwarning(message="Creator: GakPower\nVersion: 1.0.0\nCreated at 1/5/2017")

info_menu.add_command(label="About", command=show_about)