Skip to content Skip to sidebar Skip to footer

How To Set The Position Of A Messagebox Using Tkinter

I have been looking all over to see if I can find any help with this and haven't gotten anywhere My program is a simple tkinter menu that is set to be in a default position in the

Solution 1:

You will need to build a custom Toplevel() window and then tell it to re-position to the corner of the root window. We can do this with the Toplevel() class and the winfo() methods.

import tkinter as tk
# import Tkinter as tk # for Python 2.X


class MessageWindow(tk.Toplevel):
    def __init__(self, title, message):
        super().__init__()
        self.details_expanded = False
        self.title(title)
        self.geometry("300x75+{}+{}".format(self.master.winfo_x(), self.master.winfo_y()))
        self.resizable(False, False)
        self.rowconfigure(0, weight=0)
        self.rowconfigure(1, weight=1)
        self.columnconfigure(0, weight=1)
        self.columnconfigure(1, weight=1)
        tk.Label(self, text=message).grid(row=0, column=0, columnspan=3, pady=(7, 7), padx=(7, 7), sticky="ew")
        tk.Button(self, text="OK", command=self.master.destroy).grid(row=1, column=1, sticky="e")
        tk.Button(self, text="Cancel", command=self.destroy).grid(row=1, column=2, padx=(7, 7), sticky="e")

root = tk.Tk()
root.geometry("300x224")
root.resizable(0, 0)

def yes_exit():
    print("do other stuff here then root.destroy")
    root.destroy()

def exit_root():
    MessageWindow("Quit", "Are you sure you want to quit?")

root.protocol("WM_DELETE_WINDOW", exit_root)
root.mainloop()

Results:

enter image description here

Personally I would build this all in one class inheriting from Tk(), make the buttons even with ttk buttons and use a label to reference the built in question image located at ::tk::icons::question like this:

import tkinter as tk
import tkinter.ttk as ttk
# import Tkinter as tk # for Python 2.X

class GUI(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry("300x224")
        self.resizable(0, 0)
        self.protocol("WM_DELETE_WINDOW", self.exit_window)

    def yes_exit(self):
        print("do other stuff here then self.destroy")
        self.destroy()

    def exit_window(self):
        top = tk.Toplevel(self)
        top.details_expanded = False
        top.title("Quit")
        top.geometry("300x100+{}+{}".format(self.winfo_x(), self.winfo_y()))
        top.resizable(False, False)
        top.rowconfigure(0, weight=0)
        top.rowconfigure(1, weight=1)
        top.columnconfigure(0, weight=1)
        top.columnconfigure(1, weight=1)
        tk.Label(top, image="::tk::icons::question").grid(row=0, column=0, pady=(7, 0), padx=(7, 7), sticky="e")
        tk.Label(top, text="Are you sure you want to quit?").grid(row=0, column=1, columnspan=2, pady=(7, 7), sticky="w")
        ttk.Button(top, text="OK", command=self.yes_exit).grid(row=1, column=1, sticky="e")
        ttk.Button(top, text="Cancel", command=top.destroy).grid(row=1, column=2, padx=(7, 7), sticky="e")

if __name__ == "__main__":
    GUI().mainloop()

Results:

enter image description here

Post a Comment for "How To Set The Position Of A Messagebox Using Tkinter"