Question about lining up frames in Tkinter within a grid, attempting to achieve identical results with...
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
python python-3.x macos tkinter
asked Nov 21 at 21:10
Jonathan
314
314
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
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()
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 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 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 at 21:54
I updated my answer with a small code change that removes the gaps between the labels
– Felix
Nov 21 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 at 22:21
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
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()
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 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 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 at 21:54
I updated my answer with a small code change that removes the gaps between the labels
– Felix
Nov 21 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 at 22:21
add a comment |
up vote
2
down vote
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()
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 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 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 at 21:54
I updated my answer with a small code change that removes the gaps between the labels
– Felix
Nov 21 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 at 22:21
add a comment |
up vote
2
down vote
up vote
2
down vote
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()
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()
edited Nov 21 at 21:59
answered Nov 21 at 21:32
Felix
2,6172929
2,6172929
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 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 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 at 21:54
I updated my answer with a small code change that removes the gaps between the labels
– Felix
Nov 21 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 at 22:21
add a comment |
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 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 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 at 21:54
I updated my answer with a small code change that removes the gaps between the labels
– Felix
Nov 21 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 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 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 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 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 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 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 at 21:54
I updated my answer with a small code change that removes the gaps between the labels
– Felix
Nov 21 at 22:00
I updated my answer with a small code change that removes the gaps between the labels
– Felix
Nov 21 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 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 at 22:21
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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