0

i've a tkinter toplevel widget were you can choose ok or accept. it is created in a function: if you call that funtion the program should wait for a click on a button and than the function should return that choise, so that my function have the same use a the input function in python 3. now my problem is: how can i pause my script until the user clicked on a button, so that all other files in my project pause (there happens nothing if the user clicks on a button in another tkinter window) are paused?

"""this is a script to alert a message in a tkinter toplevel window."""
from tkinter import Toplevel, Text, Button
import mouse as ms
root = None


def alert(msg, choice=False, button_a_msg="Ok", button_b_msg="cancel"):
    try:
        global root
        if root is not None:
            root.destroy() #make sure that there is no other duplicate of this window
        clicked = False
        chosen = None
        root = Toplevel()
        root.geometry("100x75")
        text = Text(root)
        text.insert("1.0", msg)
        if choice:
            button_a = Button(root, text=button_a_msg, command=lambda: [exec("clicked = True"), exec("chosen = 'a'")])
            button_b = Button(root, text=button_b_msg, command=lambda: [exec("clicked = True"), exec("chosen = 'b'")])
        else:
            pass
        text.config(state="disabled")
        text.pack(expand=True, fill="both")
        #insert code to wait here
        return chosen
    except Exception:
        pass

i tried to find a function in the mouse module so i can set up a while loop and simply wait until mouse click and then check if a button is clicked, but it seems that that not exists.

  • Isn't tkInter asynchronous (event-driven)? You may want to try A) disabling all other inputs/buttons/views/etc. _before_ popping up the [blocking] dialog, and B) re-enabling them after an option is chosen (right before/after `alert()` returns). – Adam Smooch Jan 16 '23 at 16:35
  • that sounds usefull, but when you have roughly 25 tkinter widgets to block (i'am not a robot) is it less usefull, something like unpossible – Jasper Bart Jan 17 '23 at 15:35

0 Answers0