Tkinter: How to create popup Window or Messagebox
This example shows how to popup Toplevel
window or Messagebox
.
Second window uses Toplevel
instead of Tk
and it doesn't need mainloop
.
import tkinter as tk
from tkinter.messagebox import showinfo
# --- functions ---
def popup_window():
window = tk.Toplevel()
label = tk.Label(window, text="Hello World!")
label.pack(fill='x', padx=50, pady=5)
button_close = tk.Button(window, text="Close", command=window.destroy)
button_close.pack(fill='x')
def popup_showinfo():
showinfo("ShowInfo", "Hello World!")
# --- main ---
root = tk.Tk()
button_bonus = tk.Button(root, text="Window", command=popup_window)
button_bonus.pack(fill='x')
button_showinfo = tk.Button(root, text="ShowInfo", command=popup_showinfo)
button_showinfo.pack(fill='x')
button_close = tk.Button(root, text="Close", command=root.destroy)
button_close.pack(fill='x')
root.mainloop()
The same using class. All is hidden in __init__
.
If App()
would have more methods then probably it would need self.root
instead of root
.
import tkinter as tk
from tkinter.messagebox import showinfo
# --- classes ---
class App():
def __init__(self):
root = tk.Tk()
button_bonus = tk.Button(root, text="Window", command=self.popup_window)
button_bonus.pack(fill='x')
button_showinfo = tk.Button(root, text="ShowInfo", command=self.popup_showinfo)
button_showinfo.pack(fill='x')
button_close = tk.Button(root, text="Close", command=root.destroy)
button_close.pack(fill='x')
root.mainloop()
def popup_window(self):
window = tk.Toplevel()
label = tk.Label(window, text="Hello World!")
label.pack(fill='x', padx=50, pady=5)
button_close = tk.Button(window, text="Close", command=window.destroy)
button_close.pack(fill='x')
def popup_showinfo(self):
showinfo("ShowInfo", "Hello World!")
# --- main ---
App()
The same but App
can use root
or Toplevel
as master.
import tkinter as tk
from tkinter.messagebox import showinfo
# --- classes ---
class App():
def __init__(self, master):
self.master = master
button_bonus = tk.Button(master, text="Window", command=self.popup_window)
button_bonus.pack(fill='x')
button_showinfo = tk.Button(master, text="ShowInfo", command=self.popup_showinfo)
button_showinfo.pack(fill='x')
button_close = tk.Button(master, text="Close", command=master.destroy)
button_close.pack(fill='x')
def popup_window(self):
window = tk.Toplevel()
label = tk.Label(window, text="Hello World!")
label.pack(fill='x', padx=50, pady=5)
button_close = tk.Button(window, text="Close", command=window.destroy)
button_close.pack(fill='x')
def popup_showinfo(self):
showinfo("ShowInfo", "Hello World!")
# --- main ---
root = tk.Tk()
app = App(root)
root.mainloop()
You can also create separated class with popup window.
It needs self.root
instead of root
because it is used in run()
and popup_window()
.
If PopupWindow()
would have more methods then probably it would need self.window
instead of window
.
import tkinter as tk
from tkinter.messagebox import showinfo
# --- classes ---
class PopupWindow():
def __init__(self, master):
#self.master = master
window = tk.Toplevel(master)
label = tk.Label(window, text="Hello World!")
label.pack(fill='x', padx=50, pady=5)
button_close = tk.Button(window, text="Close", command=window.destroy)
button_close.pack(fill='x')
class App():
def __init__(self):
self.root = tk.Tk()
button_bonus = tk.Button(self.root, text="Window", command=self.popup_window)
button_bonus.pack(fill='x')
button_showinfo = tk.Button(self.root, text="ShowInfo", command=self.popup_showinfo)
button_showinfo.pack(fill='x')
button_close = tk.Button(self.root, text="Close", command=self.root.destroy)
button_close.pack(fill='x')
def run(self):
self.root.mainloop()
def popup_window(self):
PopupWindow(self.root)
def popup_showinfo(self):
showinfo("ShowInfo", "Hello World!")
# --- main ---
app = App()
app.run()
If you like it
Buy a Coffee
Buy a Coffee