Search code examples
pythonpython-3.xfunctiontkintermessagebox

Python messagebox program


I need some help..

I want to make a python program that uses tkinter to show a message box. When the message box is closed once, i want to make 2 boxes appear. If the 2 boxes get closed i want 4 boxes to appear and so on...

I got the first message box to appear. If you close it a for loop wil start and activate the function that opens the new message boxes. Sadly it only opens 1 at a time, because I don't really know how to activate a function more than once at the same time.

Here is my code:

import tkinter as tk
from tkinter import messagebox

root = tk.Tk()
x = 5

def open():
    messagebox.showwarning("Dragons..", "Cut off 1 head of the dragon and 2 more will appear..")

def on_closing():
    if messagebox.showwarning("Dragons..", "Cut off 1 head of the dragon and 2 more will appear.."):
        for i in range(x):
            open()
        else:
            root.destroy()

on_closing()

Can someone give me a push in the right direction?

kind regards,

Sleek


Solution

  • The messagebox are modal windows, they stay open and block your code until the user closes them.

    I you want to open several in parallel you need to do this through multiple threads.

    You can try something like:

    import threading
    
    for i in range(x):
        threading.Thread(target = open).start()
    

    If multithreading does not work then multiprocessing is the way to go:

    from multiprocessing import Process
    for i in range(x):
        p = Process(target=open).start()