Tkinter messagebox not behaving like a modal dialog

2024/7/4 16:24:16

I am using messagebox for a simple yes/no question but that question should not be avoided so I want to make it unavoidable and it seems if I have one question box.

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

Then I can go back to the root window of tkinter with the question still waitng for response, but if I have

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

With the first messagebox open I can still go back to the root window of tkinter but with the other questionbox I am unable to ( like I need). This applies to every messagebox I tested. Can anybody explain me why that is and how can I make the first question box unavoidable or I just have to do a blank messagebox before my actual question box. Is there anything I am doing wrong, because I think message box should not care if there has been a message box before it.

To illustrate my point better, I started to put together a simple nicely organised example, and it worked perfectly. I figured out what were the differences, as I started to use messagebox for the first time, I wanted to test its capabilities, and did not put it in a function. In a function it works perfectly.

Answer

Use grab_setto keep the focus off root until the messagebox has been answered. Alternatively call wait_window() after opening the messagebox. Only need 1 or the other

import tkinter as tk
from tkinter.messagebox import askyesnodef onClick():root.grab_set() # Prevent clicking root while messagebox is openans = askyesno('Confirm', 'Press Yes / No')root.wait_window() # Prevent clicking root while messagebox is openif ans:print('Yes Pressed')else:print('No Pressed')root = tk.Tk()tk.Button(root, text='Click me', command=onClick).pack()root.mainloop()
https://en.xdnf.cn/q/73245.html

Related Q&A

Cannot redirect when Django model class is mocked

I have a view here which adds a new List to the database and redirects to the List page. I have get_absolute_url configured in the model class. It seems to works perfectly.def new_list(request):form = …

How to make a Pygame Zero window full screen?

I am using the easy-to-use Python library pgzero (which uses pygame internally) for programming games.How can I make the game window full screen?import pgzrunTITLE = "Hello World"WIDTH = 80…

Check if one series is subset of another in Pandas

I have 2 columns from 2 different dataframes. I want to check if column 1 is a subset of column 2.I was using the following code:set(col1).issubset(set(col2))The issue with this is that if col1 has onl…

How to change the head size of the double head annotate in matplotlib?

Below figure shows the plot of which arrow head is very small...I tried below code, but it didnot work... it said " raise AttributeError(Unknown property %s % k) AttributeError: Unknown propert…

Passing a firefox profile to remote webdriver firefox instance not working

Im trying to start up a remote webdriver instance of Firefox and pass in a profile.profile = webdriver.FirefoxProfile() profile.set_preference("browser.download.folderList","2") sel…

Tensorflow 2.3.0 does not detect GPU

The tensorflow does not detect the GPU card. I have following the procedures suggest at Nvidia website and tensorflow/install/gpu. How can I fix it? I am using the following packages and drives: NVIDI…

How to create dict from class without None fields?

I have the following dataclass:@dataclass class Image:content_type: strdata: bytes = bid: str = ""upload_date: datetime = Nonesize: int = 0def to_dict(self) -> Dict[str, Any]:result = {}if…

Is sys.exit equivalent to raise SystemExit?

According to the documentation on sys.exit and SystemExit, it seems thatdef sys.exit(return_value=None): # or return_value=0raise SystemExit(return_value)is that correct or does sys.exit do something …

Vertical overflow of table in live display should scroll the content

Im using a Live display to show the content of a Table which grows over time. Eventually there is a vertical overflow and in that case Id like the oldest (i.e. topmost) rows to vanish while the most re…

Reading KML Files Using Fastkml

Ive searched around quite a bit for this specific Python module, and havent been able to find a source which points me in the right direction.Im trying to read a KML file and display all of the feature…