简体   繁体   中英

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

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?

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM