Skip to content Skip to sidebar Skip to footer

Copying From Messagebox With Tkinter

I've written a password generator with Tkinter and have set a messagebox that pops-up when the data for the website is already in the database. Is there an option that allows me to

Solution 1:

Try something like this:

import tkinter as tk


classPopup:
    def__init__(self, title:str="Popup", message:str="", master=None):
        if master isNone:
            # If the caller didn't give us a master, use the default one instead
            master = tk._get_default_root()

        # Create a toplevel widget
        self.root = tk.Toplevel(master)
        # A min size so the window doesn't start to look too bad
        self.root.minsize(200, 40)
        # Stop the user from resizing the window
        self.root.resizable(False, False)
        # If the user presses the `X` in the titlebar of the window call# self.destroy()
        self.root.protocol("WM_DELETE_WINDOW", self.destroy)
        # Set the title of the popup window
        self.root.title(title)

        # Calculate the needed width/height
        width = max(map(len, message.split("\n")))
        height = message.count("\n") + 1# Create the text widget
        self.text = tk.Text(self.root, bg="#f0f0ed", height=height,
                            width=width, highlightthickness=0, bd=0,
                            selectbackground="orange")
        # Add the text to the widget
        self.text.insert("end", message)
        # Make sure the user can't edit the message
        self.text.config(state="disabled")
        self.text.pack()

        # Create the "Ok" button
        self.button = tk.Button(self.root, text="Ok", command=self.destroy)
        self.button.pack()

        # Please note that you can add an icon/image here. I don't want to# download an image right now.
        ...

        # Make sure the user isn't able to spawn new popups while this is# still alive
        self.root.grab_set()
        # Stop code execution in the function that called us
        self.root.mainloop()

    defdestroy(self) -> None:
        # Stop the `.mainloop()` that's inside this class
        self.root.quit()
        # Destroy the window
        self.root.destroy()


defshow_popup():
    print("Starting popup")
    Popup(title="title", message="Message on 1 line", master=root)
    print("Ended popup")
    print("Starting popup")
    Popup(title="title", message="Message\nOn 2 lines", master=root)
    print("Ended popup")

root = tk.Tk()
root.geometry("300x300")

button = tk.Button(root, text="Click me", command=show_popup)
button.pack()

root.mainloop()

It's just a simple class that behaves a lot like messagebox.showinfo. You can add an icon if you want. Please note that some of the functionality is missing but it should work with your code.

For more info on the functions that I used please read the docs. Here are the unofficial ones.

Post a Comment for "Copying From Messagebox With Tkinter"