Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter focus on popup window

I have a question regarding a popup window from the main window. How do I ensure that when there's a popup window (to set date & time), the main window cannot be touched (i.e closed or press anything) until the user close the popup window.

I have tried using grab_set but the main window can still be closed which result in error message:

bgerror failed to handle background error.

grab_set_global work for me but I won't be able to move the popup window around.

# Main window
root = Tk()
root.title("Restaurants")
root.geometry("800x500")

lines of codes..... where user will select if they want to set the date 
and time

# Popup window
def date_time():
    popup = Tk()
    popup.title("Set Date and Time")
    popup.geometry("500x500")
    popup.grab_set()  # Not working

    lines of codes to run

I want it to focus on the popup window and the main window under it will not be able to close until the popup window is closed/destroyed.

like image 352
Barnacle Own Avatar asked Oct 15 '25 13:10

Barnacle Own


2 Answers

Ok, I have managed to solve my problem by changing the popup = Tk() to popup = Toplevel() and popup.grab_set works on the popup window. The main window can't be touched till the popup window is closed.

like image 64
Barnacle Own Avatar answered Oct 18 '25 08:10

Barnacle Own


You can use popup.focus_force, but probably first check if root is in focus. But that seems to be similar to cheating.

like image 25
rizerphe Avatar answered Oct 18 '25 07:10

rizerphe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!