0

I am trying to create a GUI for displaying data in a way that the user can flip through pages using the page buttons on the bottom (page 1, 2, 3, etc.) The problem is currently the entire window I made has two columns of equal width. If I add buttons that say "1" "2" "3" "4" they get spread out in the two columns. I tried the sticky option but that still doesn't look right. I want the four buttons placed right next to each other in the middle of the bottom of my window. Is there some way I can do this without having to change the number of columns above?

I tried creating a frame and trying to see if I could fit more columns in that frame, but it ends up just adding those columns to the right of the other two columns.

    fr=tk.Frame(master).grid(row=23,column=0, rowspan=1, columnspan=4)
    b=tk.Button(fr,text='1',command=page1)
    b.grid(row=23, column=1, sticky=tk.W)
    b1=tk.Button(fr,text='2', command=page2)
    b1.grid(row=23, column=1, sticky=tk.E)
    b2=tk.Button(fr,text='3', command=page3)
    b2.grid(row=23, column=2, sticky=tk.W)
    b3=tk.Button(fr,text='4', command=page4)
    b3.grid(row=23, column=2, sticky=tk.E)
Simon-S
  • 1
  • 2

1 Answers1

1

You can simply create a new frame and give it your main frame as parent, then inside of it you can create how many columns you want and play around with it. Here is a simple example:

app = tk.Tk()
fr=tk.Frame(app)
fr.grid(rowspan=2, columnspan=2)

# this represents what you have in the page above
something_large = tk.Button(fr, text="HELLO WORLD")
something_large_too = tk.Button(fr, text="Hello world")
something_large.grid(row=0, column=0)
something_large_too.grid(row=0, column=1)

bottom_frame = tk.Frame(fr)
bottom_frame.grid(row=1, columnspan=2)
b=tk.Button(bottom_frame,text='1')
b.grid(row=0, column=0)
b1=tk.Button(bottom_frame,text='2')
b1.grid(row=0, column=1,)
b2=tk.Button(bottom_frame,text='3')
b2.grid(row=0, column=2)
b3=tk.Button(bottom_frame,text='4')
b3.grid(row=0, column=3)
app.mainloop()

If you want the buttons to have some space between them just change their padx property when you grid them.

  • Thank you so much, this helps. I just replicated your method and it finally placed the buttons close together; however when I add different column numbers, it keeps placing them in the same spot. For example, I tried button 1 in column 0, button 2 in column, 5 and button 3 in column 10. It still placed them all close together in the same spot as if I had not changed the column numbers. Do you know why that could be happening? I even tried adding weight to ten columns but that didn't work. – Simon-S Jun 27 '21 at 20:38
  • I think this could be what you're looking for. https://stackoverflow.com/q/28019402/13962302 – Giuseppe Gadola Jun 28 '21 at 16:10