Search on blog:

Tkinter: Jak stworzyć wyskakujące okno lub Messagebox

Ten przykład pokazuje jak pokazać okno Toplevel lub Messagebox.

Drugie okno używa Toplevel zamiast Tk i nie wymaga 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()

To samo ale z użyciek klasy. Wszystko jest schowane w __init__.

Gdyby App() miało więcej metod to prawdopodobnie wymagało by self.root zamiast 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()

To samo ale App może użyć root albo Toplevel jako 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()

Można także stworzyć osobną klasę z wyskakującym oknem.

Tym razem wymaga self.root zamiast root ponieważ potrzebuje go w run() i popup_window().

Gdyby PopupWindow() miało więcej metod to prawdopodobnie wymagało by self.window zamiast 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