Creating buttons dependent on input, getting user data
Question: Relative noob to Python, still learning all the ins and outs, but I'm
learning. I'm diving into GUI for the first time for a personal project I'm
working on. (I'm a linguistics grad student and this will greatly improve my
ability to research.) I know about Tkinter and the Button class (basically,
that they exist), but I need some help to get me started. I think once I know
the magic words, I'll be able to adapt it to the situation I need.

Basically, I have sample text excerpt of about 180 words. What I am looking to
do is figure out a way to create a GUI interface such that each individual
word in the 180-word excerpt appears as a separate button, and the user is
prompted to, for example, click the verb. The value that is clicked gets
stored, and I then go on to whatever my next question is.

What I need to know: How to create the buttons depending on whatever the text
is. (I'm assuming each button will need a different variable name.) -Would it
matter if the length of one excerpt differs from another? (I'm assuming not.)
-Would it matter if there are several of the same words within the excerpt?
(I'm assuming not, since you could use indexing to remember where the word
clicked is in the original excerpt.) How to get the data stored depending on
the button clicked. How to clean the slate and go on to whatever my next
question would be.

Thanks in advance for your help.


Answer: This is a small example and demo -- it has everything you need to start your
program. See the comments inside the code:

![enter image description here](http://i.stack.imgur.com/vvdZM.png)

    
    
    import tkinter
    
    app = tkinter.Tk()
    
    # Create a set for all clicked buttons (set prevents duplication)
    clicked = set()
    # Create a tuple of words (your 180 verb goes here)
    words = 'hello', 'world', 'foo', 'bar', 'baz', 'egg', 'spam', 'ham'
    
    # Button creator function
    def create_buttons( words ):
        # Create a button for each word
        for word in words:
            # Add text and functionality to button and we are using a lambda
            # anonymous function here, but you can create a normal 'def' function
            # and pass it as 'command' argument
            button = tkinter.Button( app,
                                     text=word,
                                     command=lambda w=word: clicked.add(w) )
            # If you have 180 buttons, you should consider using the grid()
            # layout instead of pack() but for simplicity I used this one for demo
            button.pack()
    
    # For demo purpose I binded the space bar, when ever
    # you hit it, the app will print you out the 'clicked' set
    app.bind('<space>', lambda e: print( *clicked ))
    
    # This call creates the buttons
    create_buttons( words )
    
    # Now we enter to event loop -> the program is running
    app.mainloop()
    

* * *

_**EDIT:_**

Here is the code without the _lambda expressions_ :

    
    
    import tkinter
    
    app = tkinter.Tk()
    
    # Create a set for all clicked buttons (set prevents duplication)
    clicked = set()
    # Create a tuple of words (your 180 verb goes here)
    words = 'hello', 'world', 'foo', 'bar', 'baz', 'egg', 'spam', 'ham'
    
    # This function will run when pressing the space bar
    def on_spacebar_press( event ):
        print( 'Clicked words:', *clicked )
    
    # Button creator function
    def create_buttons( words ):
        # Create a button for each word
        for word in words:
            # This function will run when a button is clicked
            def on_button_click(word=word):
                clicked.add( word )
            # Add button
            button = tkinter.Button( app,
                                     text=word,
                                     command=on_button_click )
            # If you have 180 buttons, you should consider using the grid()
            # layout instead of pack() but for simplicity I used this one for demo
            button.pack()
    
    # Binding function tp space bar event
    app.bind('<space>', on_spacebar_press)
    
    # This call creates the buttons
    create_buttons( words )
    
    # Now we enter to event loop -> the program is running
    app.mainloop()
    

