1

Hello I am using pydroid 3

I'm working in a mini project on my own to learn more about gui and python

It's a mind game the program will randomly choose a number up to 9 there are 9 buttons you need to remember the chosen button by system and press it and the program will choose another and you need to press those 2 in order and so on

Right now I am having trouble in the beginning, I saw somewhere someone used a loop to make a bunch of buttons at once I tried that in my own way but I have trouble with button names in command

This is the code:

from tkinter import*

window=Tk()
buttons=[1,2,3,4,5,6,7,8,9]

#we are going to have 9 buttons

tframe=Frame(window)
mframe=Frame(window)
bframe=Frame(window)

#buttons are going to be in these frames

tframe.pack(padx=150,pady=(150,0),fill='both')
mframe.pack(padx=150,fill='both')
bframe.pack(padx=150,pady=(0,150),fill='both')

moves=[1]

def move():
    if int(name['text'])==moves[0]:
        name.config(bg='lime')
        name['state']='disable'
        
#this needs to be fixed

counter=0
for num in buttons:
    name=str('btn'+str(buttons[counter]))
    if num<4:
        name=Button(tframe,text=buttons[counter],activebackground='cyan',command=move)
    elif num>3 and num<7:
        name=Button(mframe,text=buttons[counter],activebackground='cyan',command=move)
    else:
        name=Button(bframe,text=buttons[counter],activebackground='cyan',command=move)
    name.pack(side='left',ipadx=100,ipady=100)
    counter+=1
    
#this is the loop that makes all buttons for us and set them in they're own frames nicely






    
window.mainloop()

I have problem in move function

If the user pressed the button that have 1 as text the button is supposed to change background color to lime and get disabled

I don't know what is the problem the names of the buttons or something else I tried to config name and btn1 both not work

  • 1
    Does this answer your question? [tkinter creating buttons in for loop passing command arguments](https://stackoverflow.com/questions/10865116/tkinter-creating-buttons-in-for-loop-passing-command-arguments) – jasonharper Sep 06 '22 at 15:29
  • Ok so is your problem just in the creating of the buttons in a frame? – mabski Sep 06 '22 at 15:29
  • No its in commanding them to change background if user pressed the right button – lila golestani Sep 06 '22 at 15:33

0 Answers0