How can I pop up a messagebox from a menu?

659 Views Asked by At

I am a newbie in Python. I want to create a program with Tkinter that takes the entry from the entry "box" and then compares each character of it with the charset and finally pops up a messagebox that shows the phrase. I have almost complete it but I can not make this line work:

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

Full code:

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

There are 1 best solutions below

0
On

As furas said in the comments, command= expects a function. So you should replace

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

by something like

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)