0

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!

xlmaster
  • 659
  • 7
  • 23
  • 1
    Look into wait_variable but be aware of what it actually does. [For more](https://stackoverflow.com/q/74370984/13629335) – Thingamabobs Feb 04 '23 at 17:43
  • 1
    _"...but was not able to implement in my situation..."_ - why not? How do you want the program to know when the user is finished entering data? Do you want a button? A time limit? Waiting for them to press the return key? – Bryan Oakley Feb 04 '23 at 18:04
  • I would like the user to press Enter key without time limit, and only after then script to continue run. Without button – xlmaster Feb 04 '23 at 18:09

1 Answers1

1

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()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685