Home > OS >  How can I make a Toplevel window pop up on start up of the application?
How can I make a Toplevel window pop up on start up of the application?

Time:06-15

So, I am using tkinter and would need a pop up toplevel window that asks the user what database they would like to use. However I seem to be unable to have the toplevel activate automatically on start up. If I call a function that would open up the toplevel right after calling root.mainloop() nothing opens up.


def chooseDatabase():
    top = Toplevel(root)

    choices = db.getDatabases()
    choices_to_string = []
    for c in choices:
        choices_to_string.append(c.getName())

    variable = StringVar()
    variable.set(choices_to_string[0])
    option = OptionMenu(top, variable, *choices_to_string, command=lambda x:[setDatabaseToUse(variable.get()), top.destroy()])
    option.pack()
    
    top.mainloop()



def main():

    updateList()
    root.mainloop()
    chooseDatabase()

However if I bind the chooseDatabase() function to a button it happily opens the top level when the button is pressed after start up.

What is causing this and how can I circumvent it?

CodePudding user response:

What comes after root.mainloop gets executed once you close the application (see here for more info). You should try using root.after, so that you schedule your intention to run chooseDatabase after a very small delay from app start.

def main():
    updateList()
    root.after(50, chooseDatabase)
    root.mainloop()
  • Related