How do I create a popup window in tkinter?

I found 3 mistakes

  • use Toplevel() instead of Tk() to create second/third window
  • command= expects callback – function name without ()
    (but you use popupBonusWindow.destroy())
  • don’t mix pack() and grid() in one window or frame
    (but you use grid() and pack() in popup)

But you can also use built-in messageboxes like showinfo()

import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo

def popup_bonus():
    win = tk.Toplevel()
    win.wm_title("Window")

    l = tk.Label(win, text="Input")
    l.grid(row=0, column=0)

    b = ttk.Button(win, text="Okay", command=win.destroy)
    b.grid(row=1, column=0)

def popup_showinfo():
    showinfo("Window", "Hello World!")

class Application(ttk.Frame):

    def __init__(self, master):
        ttk.Frame.__init__(self, master)
        self.pack()

        self.button_bonus = ttk.Button(self, text="Bonuses", command=popup_bonus)
        self.button_bonus.pack()

        self.button_showinfo = ttk.Button(self, text="Show Info", command=popup_showinfo)
        self.button_showinfo.pack()

root = tk.Tk()

app = Application(root)

root.mainloop()

BTW: I put it on page: Tkinter: How to create popup Window or Messagebox

Leave a Comment