0

this is my first time ever coding in python and I'm trying to make a grid of buttons each with a word on it. I want to store the word that is on the button in an array when the user presses the button, how can I go about doing this? any advice is much appreciated. I'm trying to append the word into an array but it's putting the whole list of names into that array, not just the button I press but the whole list of words.

import tkinter as tk
from tkinter import *
from tkinter import ttk


root = tk.Tk()
list = []
root.title("grid() method")
root.geometry("390x844") 
frame = tk.Frame(root, bg="white")
frame.place(relwidth=390,relheight=844)

#the text on buttons
Word =[['Adventurous', 'Creative', 'Artistic', "Curious", "Abstract"],
    ["Self-Disciplined", "Organized", "Plan-Driven", "Aware", "Detail Focused"],
    ["Energetic", "Center of Attention", "Assertive", "Impulsive", "Sociable"],
    ["Empathetic", "Trusting", "Honest", "Helping", "Put others first"],
    ["Stress Easy", "Moody", "Worrisome", "Emotionally Unstable", "Irritable"]]

#making 1 button per element in Word     
for w in range(len(Word)):
    for a in range(len(Word)):
        button1 = tk.Button(root,height =4,width=5,bd=6,padx=18.50,pady=18.50)
        button1.grid(row=w,column=a)
        button1.config(text=Word[w][a], justify =LEFT, command= list.append(Word[w][a])) #trying to put the word on the button into list[]
        
root.mainloop()
print(list)
acw1668
  • 40,144
  • 5
  • 22
  • 34
chanu
  • 33
  • 6
  • 1
    Does this answer your question? [Why is my Button's command executed immediately when I create the Button, and not when I click it?](https://stackoverflow.com/questions/5767228/why-is-my-buttons-command-executed-immediately-when-i-create-the-button-and-no) – Delrius Euphoria Oct 02 '22 at 08:06
  • Note that value for `relwidth` and `relheight` options is normally from 0 (0%) to 1 (100%). So you have created a very very large frame. Also `for a in range(len(Word))` should be `for a in range(len(Word[w]))` instead. – acw1668 Oct 03 '22 at 01:40

0 Answers0