Tkinter message does not work as a modal dialog

I use Messagebox for a simple yes / no question, but this question should not be avoided, so I want to make it inevitable, and it seems that I have one questionnaire.

messagebox.askyesno("text", "question?") 

Then I can return to the root window of tkinter with the question still waitng for an answer, but if I have

 messagebox.askyesno("text", "question?") messagebox.askyesno("text", "question?") 

Having opened the first message box, I can still return to the tkinter root window, but with a different question I cannot (for example, I need to). This applies to all message boxes I tested. Can someone explain to me why this is so and how I can make the first question a question inevitable, or I just need to make an empty messagebox before my actual question. Is there something I'm doing wrong, because I think the message box should not worry if there is a message box in front of it.

To illustrate my point, I began to draw up a simple, well-organized example, and it worked great. I realized what the differences were, since I started using messagebox for the first time, I wanted to test its capabilities and did not put it in a function. In function, it works great.

+4
source share
1 answer

Use grab_set so that the focus is off the root until the message is answered. Alternatively, call wait_window() after opening the message. Only need 1 or another

 import tkinter as tk from tkinter.messagebox import askyesno def onClick(): root.grab_set() # Prevent clicking root while messagebox is open ans = askyesno('Confirm', 'Press Yes / No') root.wait_window() # Prevent clicking root while messagebox is open if ans: print('Yes Pressed') else: print('No Pressed') root = tk.Tk() tk.Button(root, text='Click me', command=onClick).pack() root.mainloop() 
0
source

Source: https://habr.com/ru/post/1439459/


All Articles