Question about lining up frames in Tkinter within a grid, attempting to achieve identical results with...












0















New to python and getting a lot of great information from this website.



I'm creating a chart with .grid() and then again with frames in .pack() as an exercise and seeing if I can get identical results.



The function showchart_grid() works and produces pretty much what I want to create. It's a chart where the bottom row is 14 squares, and the top row is labels for those squares, and sometimes the column labels span 2 squares. You can see this accounted for in the columnspan= attribute. There is some weird whitespace between the column labels in row 0 but other than that it all lines up.



def showchart_grid():
root = Tk()

# create 14 squares on the grid

for i in range(1, 15):

spot = Label(root, text='square', bg='white', height=5, width=10, borderwidth=1, relief='solid', justify=CENTER)
spot.grid(row=1, column=i)

# create the column labels on top of the grid in row one

label0 = Label(root, text=0, bg="DodgerBlue", fg="white", height=2, width=10, borderwidth=1, relief='solid')
label0.grid(row=0, column=1)
label1 = Label(root, text=1, bg='CadetBlue1', height=2, width=20, borderwidth=1, relief='solid')
label1.grid(row=0, column=2, columnspan=2)
label2 = Label(root, text=2, bg='DodgerBlue', height=2, width=20, borderwidth=1, relief='solid')
label2.grid(row=0, column=4, columnspan=2)
label4 = Label(root, text=4, bg='CadetBlue1', height=2, width=20, borderwidth=1, relief='solid')
label4.grid(row=0, column=6, columnspan=2)
label6 = Label(root, text=6, bg='DodgerBlue', height=2, width=20, borderwidth=1, relief='solid')
label6.grid(row=0, column=8, columnspan=2)
label8 = Label(root, text=8, bg='CadetBlue1', height=2, width=20, borderwidth=1, relief='solid')
label8.grid(row=0, column=10, columnspan=2)
label11 = Label(root, text=11, bg='DodgerBlue', height=2, width=20, borderwidth=1, relief='solid')
label11.grid(row=0, column=12, columnspan=2)
label15 = Label(root, text=15, bg="CadetBlue1", height=2, width=10, borderwidth=1, relief='solid')
label15.grid(row=0, column=14)

mainloop()


Now, when I attempt to do the same thing by creating a frame for the column labels and a frame for the squares, the positions don't match up anymore even though the sizes are the same. It's as if the column labels aren't filling up the whole frame. I've messed with fill= and expand= but haven't been able to figure out exactly how. Note: the weird whitespace problem between the column labels is solved in this version.



def showchart_framespack_test():
root = Tk()

# define my frames

pointsframe = Frame(root, width=140)
pointsframe.pack(side=TOP)

playerframe = Frame(root, width=140)
playerframe.pack(side=TOP)

# create bottom row of squares with pack

for i in range(1, 15):

spot = Label(playerframe, text='empty', bg='white', height=5, width=10, borderwidth=1, relief='solid', justify=CENTER)
spot.pack(side=LEFT)

# create top row of labels with pack

label0 = Label(pointsframe, text=0, bg="DodgerBlue", fg="white", height=2, width=10, borderwidth=1, relief='solid')
label0.pack(side=LEFT)
label1 = Label(pointsframe, text=1, bg='CadetBlue1', height=2, width=20, borderwidth=1, relief='solid')
label1.pack(side=LEFT)
label2 = Label(pointsframe, text=2, bg='DodgerBlue', height=2, width=20, borderwidth=1, relief='solid')
label2.pack(side=LEFT)
label4 = Label(pointsframe, text=4, bg='CadetBlue1', height=2, width=20, borderwidth=1, relief='solid')
label4.pack(side=LEFT)
label6 = Label(pointsframe, text=6, bg='DodgerBlue', height=2, width=20, borderwidth=1, relief='solid')
label6.pack(side=LEFT)
label8 = Label(pointsframe, text=8, bg='CadetBlue1', height=2, width=20, borderwidth=1, relief='solid')
label8.pack(side=LEFT)
label11 = Label(pointsframe, text=11, bg='DodgerBlue', height=2, width=20, borderwidth=1, relief='solid')
label11.pack(side=LEFT)
label15 = Label(pointsframe, text=15, bg="CadetBlue1", height=2, width=10, borderwidth=1, relief='solid')
label15.pack(side=LEFT)



mainloop()


How can I improve the programming here to get my desired result of 14 squares on the bottom, with the labels on top that each line up perfectly with the borders of the 14 squares on the bottom row?



PS these are functions because they will take inputs and adjust the chart as such, but for now I just left it as "empty" all the way across.










share|improve this question



























    0















    New to python and getting a lot of great information from this website.



    I'm creating a chart with .grid() and then again with frames in .pack() as an exercise and seeing if I can get identical results.



    The function showchart_grid() works and produces pretty much what I want to create. It's a chart where the bottom row is 14 squares, and the top row is labels for those squares, and sometimes the column labels span 2 squares. You can see this accounted for in the columnspan= attribute. There is some weird whitespace between the column labels in row 0 but other than that it all lines up.



    def showchart_grid():
    root = Tk()

    # create 14 squares on the grid

    for i in range(1, 15):

    spot = Label(root, text='square', bg='white', height=5, width=10, borderwidth=1, relief='solid', justify=CENTER)
    spot.grid(row=1, column=i)

    # create the column labels on top of the grid in row one

    label0 = Label(root, text=0, bg="DodgerBlue", fg="white", height=2, width=10, borderwidth=1, relief='solid')
    label0.grid(row=0, column=1)
    label1 = Label(root, text=1, bg='CadetBlue1', height=2, width=20, borderwidth=1, relief='solid')
    label1.grid(row=0, column=2, columnspan=2)
    label2 = Label(root, text=2, bg='DodgerBlue', height=2, width=20, borderwidth=1, relief='solid')
    label2.grid(row=0, column=4, columnspan=2)
    label4 = Label(root, text=4, bg='CadetBlue1', height=2, width=20, borderwidth=1, relief='solid')
    label4.grid(row=0, column=6, columnspan=2)
    label6 = Label(root, text=6, bg='DodgerBlue', height=2, width=20, borderwidth=1, relief='solid')
    label6.grid(row=0, column=8, columnspan=2)
    label8 = Label(root, text=8, bg='CadetBlue1', height=2, width=20, borderwidth=1, relief='solid')
    label8.grid(row=0, column=10, columnspan=2)
    label11 = Label(root, text=11, bg='DodgerBlue', height=2, width=20, borderwidth=1, relief='solid')
    label11.grid(row=0, column=12, columnspan=2)
    label15 = Label(root, text=15, bg="CadetBlue1", height=2, width=10, borderwidth=1, relief='solid')
    label15.grid(row=0, column=14)

    mainloop()


    Now, when I attempt to do the same thing by creating a frame for the column labels and a frame for the squares, the positions don't match up anymore even though the sizes are the same. It's as if the column labels aren't filling up the whole frame. I've messed with fill= and expand= but haven't been able to figure out exactly how. Note: the weird whitespace problem between the column labels is solved in this version.



    def showchart_framespack_test():
    root = Tk()

    # define my frames

    pointsframe = Frame(root, width=140)
    pointsframe.pack(side=TOP)

    playerframe = Frame(root, width=140)
    playerframe.pack(side=TOP)

    # create bottom row of squares with pack

    for i in range(1, 15):

    spot = Label(playerframe, text='empty', bg='white', height=5, width=10, borderwidth=1, relief='solid', justify=CENTER)
    spot.pack(side=LEFT)

    # create top row of labels with pack

    label0 = Label(pointsframe, text=0, bg="DodgerBlue", fg="white", height=2, width=10, borderwidth=1, relief='solid')
    label0.pack(side=LEFT)
    label1 = Label(pointsframe, text=1, bg='CadetBlue1', height=2, width=20, borderwidth=1, relief='solid')
    label1.pack(side=LEFT)
    label2 = Label(pointsframe, text=2, bg='DodgerBlue', height=2, width=20, borderwidth=1, relief='solid')
    label2.pack(side=LEFT)
    label4 = Label(pointsframe, text=4, bg='CadetBlue1', height=2, width=20, borderwidth=1, relief='solid')
    label4.pack(side=LEFT)
    label6 = Label(pointsframe, text=6, bg='DodgerBlue', height=2, width=20, borderwidth=1, relief='solid')
    label6.pack(side=LEFT)
    label8 = Label(pointsframe, text=8, bg='CadetBlue1', height=2, width=20, borderwidth=1, relief='solid')
    label8.pack(side=LEFT)
    label11 = Label(pointsframe, text=11, bg='DodgerBlue', height=2, width=20, borderwidth=1, relief='solid')
    label11.pack(side=LEFT)
    label15 = Label(pointsframe, text=15, bg="CadetBlue1", height=2, width=10, borderwidth=1, relief='solid')
    label15.pack(side=LEFT)



    mainloop()


    How can I improve the programming here to get my desired result of 14 squares on the bottom, with the labels on top that each line up perfectly with the borders of the 14 squares on the bottom row?



    PS these are functions because they will take inputs and adjust the chart as such, but for now I just left it as "empty" all the way across.










    share|improve this question

























      0












      0








      0








      New to python and getting a lot of great information from this website.



      I'm creating a chart with .grid() and then again with frames in .pack() as an exercise and seeing if I can get identical results.



      The function showchart_grid() works and produces pretty much what I want to create. It's a chart where the bottom row is 14 squares, and the top row is labels for those squares, and sometimes the column labels span 2 squares. You can see this accounted for in the columnspan= attribute. There is some weird whitespace between the column labels in row 0 but other than that it all lines up.



      def showchart_grid():
      root = Tk()

      # create 14 squares on the grid

      for i in range(1, 15):

      spot = Label(root, text='square', bg='white', height=5, width=10, borderwidth=1, relief='solid', justify=CENTER)
      spot.grid(row=1, column=i)

      # create the column labels on top of the grid in row one

      label0 = Label(root, text=0, bg="DodgerBlue", fg="white", height=2, width=10, borderwidth=1, relief='solid')
      label0.grid(row=0, column=1)
      label1 = Label(root, text=1, bg='CadetBlue1', height=2, width=20, borderwidth=1, relief='solid')
      label1.grid(row=0, column=2, columnspan=2)
      label2 = Label(root, text=2, bg='DodgerBlue', height=2, width=20, borderwidth=1, relief='solid')
      label2.grid(row=0, column=4, columnspan=2)
      label4 = Label(root, text=4, bg='CadetBlue1', height=2, width=20, borderwidth=1, relief='solid')
      label4.grid(row=0, column=6, columnspan=2)
      label6 = Label(root, text=6, bg='DodgerBlue', height=2, width=20, borderwidth=1, relief='solid')
      label6.grid(row=0, column=8, columnspan=2)
      label8 = Label(root, text=8, bg='CadetBlue1', height=2, width=20, borderwidth=1, relief='solid')
      label8.grid(row=0, column=10, columnspan=2)
      label11 = Label(root, text=11, bg='DodgerBlue', height=2, width=20, borderwidth=1, relief='solid')
      label11.grid(row=0, column=12, columnspan=2)
      label15 = Label(root, text=15, bg="CadetBlue1", height=2, width=10, borderwidth=1, relief='solid')
      label15.grid(row=0, column=14)

      mainloop()


      Now, when I attempt to do the same thing by creating a frame for the column labels and a frame for the squares, the positions don't match up anymore even though the sizes are the same. It's as if the column labels aren't filling up the whole frame. I've messed with fill= and expand= but haven't been able to figure out exactly how. Note: the weird whitespace problem between the column labels is solved in this version.



      def showchart_framespack_test():
      root = Tk()

      # define my frames

      pointsframe = Frame(root, width=140)
      pointsframe.pack(side=TOP)

      playerframe = Frame(root, width=140)
      playerframe.pack(side=TOP)

      # create bottom row of squares with pack

      for i in range(1, 15):

      spot = Label(playerframe, text='empty', bg='white', height=5, width=10, borderwidth=1, relief='solid', justify=CENTER)
      spot.pack(side=LEFT)

      # create top row of labels with pack

      label0 = Label(pointsframe, text=0, bg="DodgerBlue", fg="white", height=2, width=10, borderwidth=1, relief='solid')
      label0.pack(side=LEFT)
      label1 = Label(pointsframe, text=1, bg='CadetBlue1', height=2, width=20, borderwidth=1, relief='solid')
      label1.pack(side=LEFT)
      label2 = Label(pointsframe, text=2, bg='DodgerBlue', height=2, width=20, borderwidth=1, relief='solid')
      label2.pack(side=LEFT)
      label4 = Label(pointsframe, text=4, bg='CadetBlue1', height=2, width=20, borderwidth=1, relief='solid')
      label4.pack(side=LEFT)
      label6 = Label(pointsframe, text=6, bg='DodgerBlue', height=2, width=20, borderwidth=1, relief='solid')
      label6.pack(side=LEFT)
      label8 = Label(pointsframe, text=8, bg='CadetBlue1', height=2, width=20, borderwidth=1, relief='solid')
      label8.pack(side=LEFT)
      label11 = Label(pointsframe, text=11, bg='DodgerBlue', height=2, width=20, borderwidth=1, relief='solid')
      label11.pack(side=LEFT)
      label15 = Label(pointsframe, text=15, bg="CadetBlue1", height=2, width=10, borderwidth=1, relief='solid')
      label15.pack(side=LEFT)



      mainloop()


      How can I improve the programming here to get my desired result of 14 squares on the bottom, with the labels on top that each line up perfectly with the borders of the 14 squares on the bottom row?



      PS these are functions because they will take inputs and adjust the chart as such, but for now I just left it as "empty" all the way across.










      share|improve this question














      New to python and getting a lot of great information from this website.



      I'm creating a chart with .grid() and then again with frames in .pack() as an exercise and seeing if I can get identical results.



      The function showchart_grid() works and produces pretty much what I want to create. It's a chart where the bottom row is 14 squares, and the top row is labels for those squares, and sometimes the column labels span 2 squares. You can see this accounted for in the columnspan= attribute. There is some weird whitespace between the column labels in row 0 but other than that it all lines up.



      def showchart_grid():
      root = Tk()

      # create 14 squares on the grid

      for i in range(1, 15):

      spot = Label(root, text='square', bg='white', height=5, width=10, borderwidth=1, relief='solid', justify=CENTER)
      spot.grid(row=1, column=i)

      # create the column labels on top of the grid in row one

      label0 = Label(root, text=0, bg="DodgerBlue", fg="white", height=2, width=10, borderwidth=1, relief='solid')
      label0.grid(row=0, column=1)
      label1 = Label(root, text=1, bg='CadetBlue1', height=2, width=20, borderwidth=1, relief='solid')
      label1.grid(row=0, column=2, columnspan=2)
      label2 = Label(root, text=2, bg='DodgerBlue', height=2, width=20, borderwidth=1, relief='solid')
      label2.grid(row=0, column=4, columnspan=2)
      label4 = Label(root, text=4, bg='CadetBlue1', height=2, width=20, borderwidth=1, relief='solid')
      label4.grid(row=0, column=6, columnspan=2)
      label6 = Label(root, text=6, bg='DodgerBlue', height=2, width=20, borderwidth=1, relief='solid')
      label6.grid(row=0, column=8, columnspan=2)
      label8 = Label(root, text=8, bg='CadetBlue1', height=2, width=20, borderwidth=1, relief='solid')
      label8.grid(row=0, column=10, columnspan=2)
      label11 = Label(root, text=11, bg='DodgerBlue', height=2, width=20, borderwidth=1, relief='solid')
      label11.grid(row=0, column=12, columnspan=2)
      label15 = Label(root, text=15, bg="CadetBlue1", height=2, width=10, borderwidth=1, relief='solid')
      label15.grid(row=0, column=14)

      mainloop()


      Now, when I attempt to do the same thing by creating a frame for the column labels and a frame for the squares, the positions don't match up anymore even though the sizes are the same. It's as if the column labels aren't filling up the whole frame. I've messed with fill= and expand= but haven't been able to figure out exactly how. Note: the weird whitespace problem between the column labels is solved in this version.



      def showchart_framespack_test():
      root = Tk()

      # define my frames

      pointsframe = Frame(root, width=140)
      pointsframe.pack(side=TOP)

      playerframe = Frame(root, width=140)
      playerframe.pack(side=TOP)

      # create bottom row of squares with pack

      for i in range(1, 15):

      spot = Label(playerframe, text='empty', bg='white', height=5, width=10, borderwidth=1, relief='solid', justify=CENTER)
      spot.pack(side=LEFT)

      # create top row of labels with pack

      label0 = Label(pointsframe, text=0, bg="DodgerBlue", fg="white", height=2, width=10, borderwidth=1, relief='solid')
      label0.pack(side=LEFT)
      label1 = Label(pointsframe, text=1, bg='CadetBlue1', height=2, width=20, borderwidth=1, relief='solid')
      label1.pack(side=LEFT)
      label2 = Label(pointsframe, text=2, bg='DodgerBlue', height=2, width=20, borderwidth=1, relief='solid')
      label2.pack(side=LEFT)
      label4 = Label(pointsframe, text=4, bg='CadetBlue1', height=2, width=20, borderwidth=1, relief='solid')
      label4.pack(side=LEFT)
      label6 = Label(pointsframe, text=6, bg='DodgerBlue', height=2, width=20, borderwidth=1, relief='solid')
      label6.pack(side=LEFT)
      label8 = Label(pointsframe, text=8, bg='CadetBlue1', height=2, width=20, borderwidth=1, relief='solid')
      label8.pack(side=LEFT)
      label11 = Label(pointsframe, text=11, bg='DodgerBlue', height=2, width=20, borderwidth=1, relief='solid')
      label11.pack(side=LEFT)
      label15 = Label(pointsframe, text=15, bg="CadetBlue1", height=2, width=10, borderwidth=1, relief='solid')
      label15.pack(side=LEFT)



      mainloop()


      How can I improve the programming here to get my desired result of 14 squares on the bottom, with the labels on top that each line up perfectly with the borders of the 14 squares on the bottom row?



      PS these are functions because they will take inputs and adjust the chart as such, but for now I just left it as "empty" all the way across.







      python python-3.x macos tkinter






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 21:10









      JonathanJonathan

      314




      314
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Fundamentally, the grid is a two-dimensional layout. If two items are assigned to the same column, they line up vertically.



          On the other hand, pack is a one-dimensional layout that positions elements relative to others.



          In your second example, you're creating two horizontal lists of elements that are independent of each other. Both have the same number of items, but elements will not line up because "columns" are calculated individually for both frames.



          If you can live without cells spanning multiple columns, you could make your columns line up by creating 15 frames that each contain a label and a square and put them next to each other using pack.



          Other than that, your use-case really is what the grid layout has been designed for. I'm using grid for everything because in my opinion it's more flexible and easier to reason about.



          Does this answer your question?



          EDIT:



          You can get rid of the gaps if you use the sticky="WE" option instead of manually specifying the width of each label. This will make it span left and right. Modified code:



          from tkinter import *

          def showchart_grid():
          root = Tk()

          # create 14 squares on the grid

          for i in range(1, 15):

          spot = Label(root, text='square', bg='white', height=5, width=10, borderwidth=1, relief='solid', justify=CENTER)
          spot.grid(row=1, column=i)

          # create the column labels on top of the grid in row one

          label0 = Label(root, text=0, bg="DodgerBlue", fg="white", height=2, borderwidth=1, relief='solid')
          label0.grid(row=0, column=1, sticky="WE")
          label1 = Label(root, text=1, bg='CadetBlue1', height=2, borderwidth=1, relief='solid')
          label1.grid(row=0, column=2, sticky="WE", columnspan=2)
          label2 = Label(root, text=2, bg='DodgerBlue', height=2, borderwidth=1, relief='solid')
          label2.grid(row=0, column=4, sticky="WE", columnspan=2)
          label4 = Label(root, text=4, bg='CadetBlue1', height=2, borderwidth=1, relief='solid')
          label4.grid(row=0, column=6, sticky="WE", columnspan=2)
          label6 = Label(root, text=6, bg='DodgerBlue', height=2, borderwidth=1, relief='solid')
          label6.grid(row=0, column=8, sticky="WE", columnspan=2)
          label8 = Label(root, text=8, bg='CadetBlue1', height=2, borderwidth=1, relief='solid')
          label8.grid(row=0, column=10, sticky="WE", columnspan=2)
          label11 = Label(root, text=11, bg='DodgerBlue', height=2, borderwidth=1, relief='solid')
          label11.grid(row=0, column=12, sticky="WE", columnspan=2)
          label15 = Label(root, text=15, bg="CadetBlue1", height=2, borderwidth=1, relief='solid')
          label15.grid(row=0, column=14, sticky="WE")

          mainloop()


          if __name__ == '__main__':
          showchart_grid()





          share|improve this answer


























          • Yes definitely. Eventually I would like to have a background image for each square on the bottom row, and then have text over that image. Is that still possible within the grid method?

            – Jonathan
            Nov 21 '18 at 21:37











          • Yes, that should be possible. Assign the image and the text to the same grid position and make sure that the text is on top and has a transparent background. But there should be some other answers on this topic on SO already ;-)

            – Felix
            Nov 21 '18 at 21:51











          • Could you mark the answer as solved (it's the check mark next to the voting buttons)? This will signal to others that your question has been answered and also get the answerer (me in this case) some reputation. Thanks :-)

            – Felix
            Nov 21 '18 at 21:54











          • I updated my answer with a small code change that removes the gaps between the labels

            – Felix
            Nov 21 '18 at 22:00











          • Perfect! Thank you - actually had figured that one out :). Currently on the wild goose chase to learn how to make the background of my label transparent. looks like I have to use Canvas?

            – Jonathan
            Nov 21 '18 at 22:21











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420517%2fquestion-about-lining-up-frames-in-tkinter-within-a-grid-attempting-to-achieve%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Fundamentally, the grid is a two-dimensional layout. If two items are assigned to the same column, they line up vertically.



          On the other hand, pack is a one-dimensional layout that positions elements relative to others.



          In your second example, you're creating two horizontal lists of elements that are independent of each other. Both have the same number of items, but elements will not line up because "columns" are calculated individually for both frames.



          If you can live without cells spanning multiple columns, you could make your columns line up by creating 15 frames that each contain a label and a square and put them next to each other using pack.



          Other than that, your use-case really is what the grid layout has been designed for. I'm using grid for everything because in my opinion it's more flexible and easier to reason about.



          Does this answer your question?



          EDIT:



          You can get rid of the gaps if you use the sticky="WE" option instead of manually specifying the width of each label. This will make it span left and right. Modified code:



          from tkinter import *

          def showchart_grid():
          root = Tk()

          # create 14 squares on the grid

          for i in range(1, 15):

          spot = Label(root, text='square', bg='white', height=5, width=10, borderwidth=1, relief='solid', justify=CENTER)
          spot.grid(row=1, column=i)

          # create the column labels on top of the grid in row one

          label0 = Label(root, text=0, bg="DodgerBlue", fg="white", height=2, borderwidth=1, relief='solid')
          label0.grid(row=0, column=1, sticky="WE")
          label1 = Label(root, text=1, bg='CadetBlue1', height=2, borderwidth=1, relief='solid')
          label1.grid(row=0, column=2, sticky="WE", columnspan=2)
          label2 = Label(root, text=2, bg='DodgerBlue', height=2, borderwidth=1, relief='solid')
          label2.grid(row=0, column=4, sticky="WE", columnspan=2)
          label4 = Label(root, text=4, bg='CadetBlue1', height=2, borderwidth=1, relief='solid')
          label4.grid(row=0, column=6, sticky="WE", columnspan=2)
          label6 = Label(root, text=6, bg='DodgerBlue', height=2, borderwidth=1, relief='solid')
          label6.grid(row=0, column=8, sticky="WE", columnspan=2)
          label8 = Label(root, text=8, bg='CadetBlue1', height=2, borderwidth=1, relief='solid')
          label8.grid(row=0, column=10, sticky="WE", columnspan=2)
          label11 = Label(root, text=11, bg='DodgerBlue', height=2, borderwidth=1, relief='solid')
          label11.grid(row=0, column=12, sticky="WE", columnspan=2)
          label15 = Label(root, text=15, bg="CadetBlue1", height=2, borderwidth=1, relief='solid')
          label15.grid(row=0, column=14, sticky="WE")

          mainloop()


          if __name__ == '__main__':
          showchart_grid()





          share|improve this answer


























          • Yes definitely. Eventually I would like to have a background image for each square on the bottom row, and then have text over that image. Is that still possible within the grid method?

            – Jonathan
            Nov 21 '18 at 21:37











          • Yes, that should be possible. Assign the image and the text to the same grid position and make sure that the text is on top and has a transparent background. But there should be some other answers on this topic on SO already ;-)

            – Felix
            Nov 21 '18 at 21:51











          • Could you mark the answer as solved (it's the check mark next to the voting buttons)? This will signal to others that your question has been answered and also get the answerer (me in this case) some reputation. Thanks :-)

            – Felix
            Nov 21 '18 at 21:54











          • I updated my answer with a small code change that removes the gaps between the labels

            – Felix
            Nov 21 '18 at 22:00











          • Perfect! Thank you - actually had figured that one out :). Currently on the wild goose chase to learn how to make the background of my label transparent. looks like I have to use Canvas?

            – Jonathan
            Nov 21 '18 at 22:21
















          2














          Fundamentally, the grid is a two-dimensional layout. If two items are assigned to the same column, they line up vertically.



          On the other hand, pack is a one-dimensional layout that positions elements relative to others.



          In your second example, you're creating two horizontal lists of elements that are independent of each other. Both have the same number of items, but elements will not line up because "columns" are calculated individually for both frames.



          If you can live without cells spanning multiple columns, you could make your columns line up by creating 15 frames that each contain a label and a square and put them next to each other using pack.



          Other than that, your use-case really is what the grid layout has been designed for. I'm using grid for everything because in my opinion it's more flexible and easier to reason about.



          Does this answer your question?



          EDIT:



          You can get rid of the gaps if you use the sticky="WE" option instead of manually specifying the width of each label. This will make it span left and right. Modified code:



          from tkinter import *

          def showchart_grid():
          root = Tk()

          # create 14 squares on the grid

          for i in range(1, 15):

          spot = Label(root, text='square', bg='white', height=5, width=10, borderwidth=1, relief='solid', justify=CENTER)
          spot.grid(row=1, column=i)

          # create the column labels on top of the grid in row one

          label0 = Label(root, text=0, bg="DodgerBlue", fg="white", height=2, borderwidth=1, relief='solid')
          label0.grid(row=0, column=1, sticky="WE")
          label1 = Label(root, text=1, bg='CadetBlue1', height=2, borderwidth=1, relief='solid')
          label1.grid(row=0, column=2, sticky="WE", columnspan=2)
          label2 = Label(root, text=2, bg='DodgerBlue', height=2, borderwidth=1, relief='solid')
          label2.grid(row=0, column=4, sticky="WE", columnspan=2)
          label4 = Label(root, text=4, bg='CadetBlue1', height=2, borderwidth=1, relief='solid')
          label4.grid(row=0, column=6, sticky="WE", columnspan=2)
          label6 = Label(root, text=6, bg='DodgerBlue', height=2, borderwidth=1, relief='solid')
          label6.grid(row=0, column=8, sticky="WE", columnspan=2)
          label8 = Label(root, text=8, bg='CadetBlue1', height=2, borderwidth=1, relief='solid')
          label8.grid(row=0, column=10, sticky="WE", columnspan=2)
          label11 = Label(root, text=11, bg='DodgerBlue', height=2, borderwidth=1, relief='solid')
          label11.grid(row=0, column=12, sticky="WE", columnspan=2)
          label15 = Label(root, text=15, bg="CadetBlue1", height=2, borderwidth=1, relief='solid')
          label15.grid(row=0, column=14, sticky="WE")

          mainloop()


          if __name__ == '__main__':
          showchart_grid()





          share|improve this answer


























          • Yes definitely. Eventually I would like to have a background image for each square on the bottom row, and then have text over that image. Is that still possible within the grid method?

            – Jonathan
            Nov 21 '18 at 21:37











          • Yes, that should be possible. Assign the image and the text to the same grid position and make sure that the text is on top and has a transparent background. But there should be some other answers on this topic on SO already ;-)

            – Felix
            Nov 21 '18 at 21:51











          • Could you mark the answer as solved (it's the check mark next to the voting buttons)? This will signal to others that your question has been answered and also get the answerer (me in this case) some reputation. Thanks :-)

            – Felix
            Nov 21 '18 at 21:54











          • I updated my answer with a small code change that removes the gaps between the labels

            – Felix
            Nov 21 '18 at 22:00











          • Perfect! Thank you - actually had figured that one out :). Currently on the wild goose chase to learn how to make the background of my label transparent. looks like I have to use Canvas?

            – Jonathan
            Nov 21 '18 at 22:21














          2












          2








          2







          Fundamentally, the grid is a two-dimensional layout. If two items are assigned to the same column, they line up vertically.



          On the other hand, pack is a one-dimensional layout that positions elements relative to others.



          In your second example, you're creating two horizontal lists of elements that are independent of each other. Both have the same number of items, but elements will not line up because "columns" are calculated individually for both frames.



          If you can live without cells spanning multiple columns, you could make your columns line up by creating 15 frames that each contain a label and a square and put them next to each other using pack.



          Other than that, your use-case really is what the grid layout has been designed for. I'm using grid for everything because in my opinion it's more flexible and easier to reason about.



          Does this answer your question?



          EDIT:



          You can get rid of the gaps if you use the sticky="WE" option instead of manually specifying the width of each label. This will make it span left and right. Modified code:



          from tkinter import *

          def showchart_grid():
          root = Tk()

          # create 14 squares on the grid

          for i in range(1, 15):

          spot = Label(root, text='square', bg='white', height=5, width=10, borderwidth=1, relief='solid', justify=CENTER)
          spot.grid(row=1, column=i)

          # create the column labels on top of the grid in row one

          label0 = Label(root, text=0, bg="DodgerBlue", fg="white", height=2, borderwidth=1, relief='solid')
          label0.grid(row=0, column=1, sticky="WE")
          label1 = Label(root, text=1, bg='CadetBlue1', height=2, borderwidth=1, relief='solid')
          label1.grid(row=0, column=2, sticky="WE", columnspan=2)
          label2 = Label(root, text=2, bg='DodgerBlue', height=2, borderwidth=1, relief='solid')
          label2.grid(row=0, column=4, sticky="WE", columnspan=2)
          label4 = Label(root, text=4, bg='CadetBlue1', height=2, borderwidth=1, relief='solid')
          label4.grid(row=0, column=6, sticky="WE", columnspan=2)
          label6 = Label(root, text=6, bg='DodgerBlue', height=2, borderwidth=1, relief='solid')
          label6.grid(row=0, column=8, sticky="WE", columnspan=2)
          label8 = Label(root, text=8, bg='CadetBlue1', height=2, borderwidth=1, relief='solid')
          label8.grid(row=0, column=10, sticky="WE", columnspan=2)
          label11 = Label(root, text=11, bg='DodgerBlue', height=2, borderwidth=1, relief='solid')
          label11.grid(row=0, column=12, sticky="WE", columnspan=2)
          label15 = Label(root, text=15, bg="CadetBlue1", height=2, borderwidth=1, relief='solid')
          label15.grid(row=0, column=14, sticky="WE")

          mainloop()


          if __name__ == '__main__':
          showchart_grid()





          share|improve this answer















          Fundamentally, the grid is a two-dimensional layout. If two items are assigned to the same column, they line up vertically.



          On the other hand, pack is a one-dimensional layout that positions elements relative to others.



          In your second example, you're creating two horizontal lists of elements that are independent of each other. Both have the same number of items, but elements will not line up because "columns" are calculated individually for both frames.



          If you can live without cells spanning multiple columns, you could make your columns line up by creating 15 frames that each contain a label and a square and put them next to each other using pack.



          Other than that, your use-case really is what the grid layout has been designed for. I'm using grid for everything because in my opinion it's more flexible and easier to reason about.



          Does this answer your question?



          EDIT:



          You can get rid of the gaps if you use the sticky="WE" option instead of manually specifying the width of each label. This will make it span left and right. Modified code:



          from tkinter import *

          def showchart_grid():
          root = Tk()

          # create 14 squares on the grid

          for i in range(1, 15):

          spot = Label(root, text='square', bg='white', height=5, width=10, borderwidth=1, relief='solid', justify=CENTER)
          spot.grid(row=1, column=i)

          # create the column labels on top of the grid in row one

          label0 = Label(root, text=0, bg="DodgerBlue", fg="white", height=2, borderwidth=1, relief='solid')
          label0.grid(row=0, column=1, sticky="WE")
          label1 = Label(root, text=1, bg='CadetBlue1', height=2, borderwidth=1, relief='solid')
          label1.grid(row=0, column=2, sticky="WE", columnspan=2)
          label2 = Label(root, text=2, bg='DodgerBlue', height=2, borderwidth=1, relief='solid')
          label2.grid(row=0, column=4, sticky="WE", columnspan=2)
          label4 = Label(root, text=4, bg='CadetBlue1', height=2, borderwidth=1, relief='solid')
          label4.grid(row=0, column=6, sticky="WE", columnspan=2)
          label6 = Label(root, text=6, bg='DodgerBlue', height=2, borderwidth=1, relief='solid')
          label6.grid(row=0, column=8, sticky="WE", columnspan=2)
          label8 = Label(root, text=8, bg='CadetBlue1', height=2, borderwidth=1, relief='solid')
          label8.grid(row=0, column=10, sticky="WE", columnspan=2)
          label11 = Label(root, text=11, bg='DodgerBlue', height=2, borderwidth=1, relief='solid')
          label11.grid(row=0, column=12, sticky="WE", columnspan=2)
          label15 = Label(root, text=15, bg="CadetBlue1", height=2, borderwidth=1, relief='solid')
          label15.grid(row=0, column=14, sticky="WE")

          mainloop()


          if __name__ == '__main__':
          showchart_grid()






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 21:59

























          answered Nov 21 '18 at 21:32









          FelixFelix

          3,0412929




          3,0412929













          • Yes definitely. Eventually I would like to have a background image for each square on the bottom row, and then have text over that image. Is that still possible within the grid method?

            – Jonathan
            Nov 21 '18 at 21:37











          • Yes, that should be possible. Assign the image and the text to the same grid position and make sure that the text is on top and has a transparent background. But there should be some other answers on this topic on SO already ;-)

            – Felix
            Nov 21 '18 at 21:51











          • Could you mark the answer as solved (it's the check mark next to the voting buttons)? This will signal to others that your question has been answered and also get the answerer (me in this case) some reputation. Thanks :-)

            – Felix
            Nov 21 '18 at 21:54











          • I updated my answer with a small code change that removes the gaps between the labels

            – Felix
            Nov 21 '18 at 22:00











          • Perfect! Thank you - actually had figured that one out :). Currently on the wild goose chase to learn how to make the background of my label transparent. looks like I have to use Canvas?

            – Jonathan
            Nov 21 '18 at 22:21



















          • Yes definitely. Eventually I would like to have a background image for each square on the bottom row, and then have text over that image. Is that still possible within the grid method?

            – Jonathan
            Nov 21 '18 at 21:37











          • Yes, that should be possible. Assign the image and the text to the same grid position and make sure that the text is on top and has a transparent background. But there should be some other answers on this topic on SO already ;-)

            – Felix
            Nov 21 '18 at 21:51











          • Could you mark the answer as solved (it's the check mark next to the voting buttons)? This will signal to others that your question has been answered and also get the answerer (me in this case) some reputation. Thanks :-)

            – Felix
            Nov 21 '18 at 21:54











          • I updated my answer with a small code change that removes the gaps between the labels

            – Felix
            Nov 21 '18 at 22:00











          • Perfect! Thank you - actually had figured that one out :). Currently on the wild goose chase to learn how to make the background of my label transparent. looks like I have to use Canvas?

            – Jonathan
            Nov 21 '18 at 22:21

















          Yes definitely. Eventually I would like to have a background image for each square on the bottom row, and then have text over that image. Is that still possible within the grid method?

          – Jonathan
          Nov 21 '18 at 21:37





          Yes definitely. Eventually I would like to have a background image for each square on the bottom row, and then have text over that image. Is that still possible within the grid method?

          – Jonathan
          Nov 21 '18 at 21:37













          Yes, that should be possible. Assign the image and the text to the same grid position and make sure that the text is on top and has a transparent background. But there should be some other answers on this topic on SO already ;-)

          – Felix
          Nov 21 '18 at 21:51





          Yes, that should be possible. Assign the image and the text to the same grid position and make sure that the text is on top and has a transparent background. But there should be some other answers on this topic on SO already ;-)

          – Felix
          Nov 21 '18 at 21:51













          Could you mark the answer as solved (it's the check mark next to the voting buttons)? This will signal to others that your question has been answered and also get the answerer (me in this case) some reputation. Thanks :-)

          – Felix
          Nov 21 '18 at 21:54





          Could you mark the answer as solved (it's the check mark next to the voting buttons)? This will signal to others that your question has been answered and also get the answerer (me in this case) some reputation. Thanks :-)

          – Felix
          Nov 21 '18 at 21:54













          I updated my answer with a small code change that removes the gaps between the labels

          – Felix
          Nov 21 '18 at 22:00





          I updated my answer with a small code change that removes the gaps between the labels

          – Felix
          Nov 21 '18 at 22:00













          Perfect! Thank you - actually had figured that one out :). Currently on the wild goose chase to learn how to make the background of my label transparent. looks like I have to use Canvas?

          – Jonathan
          Nov 21 '18 at 22:21





          Perfect! Thank you - actually had figured that one out :). Currently on the wild goose chase to learn how to make the background of my label transparent. looks like I have to use Canvas?

          – Jonathan
          Nov 21 '18 at 22:21


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420517%2fquestion-about-lining-up-frames-in-tkinter-within-a-grid-attempting-to-achieve%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin