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.

4
  • Remove the while loop and replace continue and break with pass Commented Jan 30, 2022 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. Commented Jan 30, 2022 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. Commented Jan 30, 2022 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. Commented Feb 1, 2022 at 20:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.