Home > database >  Python: How to disable main window while secondary window is open (Tkinter)
Python: How to disable main window while secondary window is open (Tkinter)

Time:10-21

I would like to disable the main window of my text adventure program while the character creation window is up. I am relatively new to this. When I try my code below, the main window stays interactable even though the secondary window (creator) is open. There is no error that the console puts out, it just doesn't work is all.

def characterCreator():
    global creator
    creator = Tk()
    creator.title('Character Creator')
    creator.geometry('300x400')

    while creator.state=='normal':
        root.state='disabled'

CodePudding user response:

You should create your creator window as an instance of tkinter.Toplevel() rather than declaring a new instance of Tk() altogether, then you can use grab_set to prevent the user from interacting with the root window while the creator window is open.

import tkinter as tk


root = tk.Tk()
# usual root configs like title and geometry go here
creator = tk.Toplevel(root)  # the Toplevel window is a child of the root window
# creator geometry and title and so on go here
creator.grab_set()  # keep window on top


if __name__ == '__main__':
    root.mainloop()
  • Related