samedi 27 mai 2017

Adding an element on specific rows of a grid using Tkinter

Is there a way to add a button in specifics rows on a grid using Tkinter toolkit?

I am currently trying to get a grid setup like this, where the red rectangles are the elements of the grid (buttons). As you can see, the second line has one additional button compared to the first, and so on.

So, am looking for a way to display one additional button to specifics rows in my grid, if possible.

Here is the code :

import sys
import tkinter as tk
from tkinter import * 


fenetre = Tk()

#frame 1
Frame1 = Frame(fenetre, borderwidth=2, relief=GROOVE)
Frame1.pack(fill=BOTH)

#frame2
Frame2 = Frame(fenetre, borderwidth=2, relief=GROOVE)
Frame2.pack(fill=BOTH)

#affichage du tableau
def afficheGrille():

    tailleGrille=(int(s.get()))

    for ligne in range((tailleGrille * 2) +1):
        for colonne in range(tailleGrille):
            if(ligne == 0 or (ligne % 2) == 0):
                #boutons horizontaux
                Button(Frame2, borderwidth=1, height=1, width=8).grid(row=ligne, column=colonne)
            else:
                #boutons verticaux
                Button(Frame2, borderwidth=1, height=5, width=2).grid(row=ligne, column=colonne)



label = Label(Frame1, text="Jeu du carré")
label.pack()

boutonJvIA=Button(Frame1, text="1 joueur vs IA", width=30, command=afficheGrille)
boutonJvIA.pack()

boutonJvJ=Button(Frame1, text="1 joueur vs 1 joueur", width=30, command=afficheGrille)
boutonJvJ.pack()

s = Spinbox(Frame1, from_= 0, to = 100)
s.pack()



fenetre.mainloop()

Thank you by advance

Aucun commentaire:

Enregistrer un commentaire