0

I intend to collect names in lists that will be used in a program, one at a time. The function does return a list and the whole program runs once, but after I call the function UserInput with the button:

ttk.Button(root, text='Add place', command= lambda:UserInput()).pack(ipadx=10, ipady=10)

it remains hanging. Even if I close the window, the program doesn't move to the next line:

print('outside function',  places)

anymore.

The code is:

import tkinter as tk

from tkinter import simpledialog

from tkinter import ttk

root = tk.Tk()

def UserInput():

    places = []

    while True:

        place = simpledialog.askstring('Input', 'Enter name',
                                parent=root)

        if place == None: # 'Cancel'
            continue

        if place == '':  
            break  
        else:
            places.append(place)
    print(places)      

    return(places)

places = (UserInput())  # list of cities to visit

print('outside function',  places)


ttk.Button(root, text='Add place', command= lambda:UserInput()).pack(ipadx=10, ipady=10)

root.mainloop()

when testing:

Any help would be much appreciated. Thanking you in advance.

L F
  • 1
  • Remove the while loop and replace `continue` and `break` with `pass` – Sister Coder Jan 30 '22 at 13:06
  • Thank you for this recommendation. It does lead to a solution with a different outcome. I am looking for a solution to be able to enter several strings into a list, and to do this again and again whenever calling the function with a button. – L F Jan 30 '22 at 14:54
  • The return value from a callback triggered by button will be discarded because there is no receiver. Actually `places` can be declared as global and so it can be accessed elsewhere. – acw1668 Jan 30 '22 at 23:10
  • Thank you very much indeed for your help. This got me further. Now, what do I need to change so that the line "print('outside function', places)" executes every time I call the function with the button. – L F Feb 01 '22 at 20:15

0 Answers0