Вам нужно будет создать собственное окно Toplevel()
, а затем попросить его переместить его в угол корневого окна.Мы можем сделать это с помощью класса Toplevel()
и методов winfo()
.
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()
Результаты:

ЛичноЯ построил бы все это в одном классе, наследуемом от Tk()
, сделал бы кнопки даже с помощью кнопок ttk и использовал бы метку для ссылки на встроенное изображение вопроса, расположенное в ::tk::icons::question
, например:
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()
Результаты:
