Home > Mobile >  How can I save multiple entries to a list and then print a random entry from that list?
How can I save multiple entries to a list and then print a random entry from that list?

Time:10-06

Hello i can only seem to print letters from an entry not the full entry (sentence).

I simply want to click a button using tkinter and after typing a sentence into a box, the button will store the sentences in a list.

Then I want to create a second button that will then print a random sentence from that list.

When I try this it only prints letters of the stored sentence.

Any ideas would be greatly appreciated as I’ve looked around for the last two days before asking.

Best wishes to you all

from tkinter import *

import random

def super_function():
    out = map(Entry.get, entr)
    clear_entry_1()

def clear_entry_1():
    Entry_field.delete(0,END)

def super_function_2():
    print(random.choice(entr))

root = Tk()
root.geometry('400x400')
entr = []

for i in range(10):
    entr.append(Entry(root))

Entry_field = Entry()
Entry_field.pack()


Button1 = Button(root, text = 'Add your idea!', command = 
super_function)
Button1.pack()
Button2 = Button(root, text='Generate an idea!', 
command=super_function_2)
Button2.pack()
root.mainloop()

CodePudding user response:

The for loop is useless and should be removed as it just appends 10 invisible entries.

What you need is to append the sentence inside super_function() instead:

def super_function():
    # save the sentence
    entr.append(Entry_field.get())
    clear_entry_1()
  • Related