python tkinter wait until entry box widget has an input by user ONLY after then continue

Question:

I have a code snippet which runs perfectly. In some cases, I need user input, but there are also cases where user input is not necessary and code functions without it perfectly.
So, in that cases I create with conditional a flow where entry box widget is created and destroyed after value is get() by script. But I cannot make code to wait until to say stops(pauses) when the user has given input value then continues to run.

code is below;

varSheetname_GS = ''
if varsoundTitle_usernameHeroContainer == 'FloatingBlueRecords' or varsoundTitle_usernameHeroContainer == 'DayDoseOfHouse':
    varSheetname_GS = varsoundTitle_usernameHeroContainer
else:
    # look for sheetname as an input value entered by user
    new_sheetname_entryBox=tk.Entry(canvas2,width=30).pack()
    new_sheetname_entryBox.focus()
    var_new_sheetName =new_sheetname_entryBox.get()
    new_sheetname_entryBox.destroy()
    varSheetname_GS = var_new_sheetName  #input("Enter the sheetname in (GooSheets):")

I have looked for so_01 and so_02 which are related to topic but was not able to implement in my situation. So, anyone who would guide me towards that answers would be great from yourside.
Thanks ahead!

Asked By: xlmaster

||

Answers:

You can use the wait_window method to wait until the entry widget has been destroyed. You can then bind the return key to destroy the window. If the entry is associated with a StringVar, you can get the value after the widget has been destroyed.

The solution might look something like this:

entry_var = tk.StringVar()
new_sheetname_entryBox=tk.Entry(canvas2,width=30, textvariable=entry_var)
new_sheetname_entryBox.pack()
new_sheetname_entryBox.bind("<Return>", lambda event: new_sheetname_entryBox.destroy())
new_sheetname_entryBox.focus_set()

# wait for the entry widget to be deleted...
new_sheetname_entryBox.wait_window()

# save the value
varSheetname_GS = entry_var.get()
Answered By: Bryan Oakley
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.