z9sami7z
z9sami7z

Reputation: 25

Tkinter, Display list elements in GUI grid

Edit: Solved

I'm trying to make 2048 game, the game is separated into 2 modules. One with the rules in it and the other is made in tkinter. My list is inside the module with the rules in it. So I'd like to know if there's anyway to display the numbers inside my list on my tkinter grid. This is how it looks like now. I succeeded to fill the area where the numbers spawn but I don't know how to display them. Thanks for the help.

This is what I'm trying to get. (picture edited)

That's the function to create the grid.

def init(n):
    base=[2,4]
    creation= grid(n, 0) #create grid with value 0
    ran1, ran2= base[randrange(0,2)], base[randrange(0,2)]
    addrandom(creation,ran1) ##spawn either a 2 or a 4
    addrandom(creation,ran2) ##spawn either a 2 or a 4
    return creation

The result would look like this.

  0      2      2      0
  0      0      0      0
  0      0      0      0
  0      0      0      0

And the Tkinter looks like this:

def drawg_grid(g):     
    can.delete(ALL) #can is the canvas
    for i in range(n+1):
        can.create_line(x0+c*i, y0,x0+c*i,y0 + n*c,fill="#92877d")
        can.create_line(x0, y0+c*i,x0+n*c ,y0+c*i,fill="#92877d")

def square(g):   
     for i in range(n):
        for j in range(n):
            x=g[i][j]
            if x!=0:
                can.create_rectangle(x0 +c*j+2,y0+c*i+2,x0 +c*(j+1)-2,y0+c*(i+1)-2, fill= dico_couleurs_case.get(x), outline= dico_couleurs_case.get(x)) #premade dictionary to get the background color 

Upvotes: 1

Views: 693

Answers (1)

z9sami7z
z9sami7z

Reputation: 25

If anybody is wondering, that's what I did.

def square(g):   
     for i in range(n):
            for j in range(n):
            x=g[i][j]
            if x!=0:
                can.create_rectangle(x0 +c*j+2,y0+c*i+2,x0 +c*(j+1)-2,y0+c*(i+1)-2, fill= dico_couleurs_case.get(x), outline= dico_couleurs_case.get(x))
                can.create_text((x0 +c*j+38,y0+c*i+38), text= x, font=("Ubuntu",28,"bold"), fill= dico_couleurs_chiffre.get(x))

Upvotes: 1

Related Questions