sarbjit
sarbjit

Reputation: 3894

Using multiple frames in Tkinter is not giving the expected result

I have been learning Tkinter, i have written a small code where i want three frames to be part of an frame. Now when i execute this code line by line, then it works as expected. However on running it as a whole program there is lot of white space between frame 1 and frame2.

from Tkinter import *
tk = Tk()
tk.geometry("")
main_frame = Frame(tk)
main_frame.grid(row=0)
frame1 = Frame(main_frame, bg="blue", width=200, height=400, borderwidth =1)
frame1.grid(row=0, column=0)
frame2 = Frame(main_frame, bg="green", width=800, height=400)
frame2.grid(row=0, column=1)
frame3 = Frame(main_frame, bg="orange", width=1000, height=100)
frame3.grid(row =1)
tk.mainloop()

Other problem is if i maximize the window and change it back to original size or I stretch this main window. I see frames gets overlapped and some ghost images. Can some one explain this weird behavior.

Regards

Upvotes: 1

Views: 1757

Answers (1)

mgilson
mgilson

Reputation: 309841

The problem is with the way you're gridding it. by default, every object gets gridded into a single block. So, you're grid looks something like this:

 -------------------------------
 |    frame 1     |  frame 2   |
 -------------------------------
 |    frame 3     |  nothing   |
 -------------------------------

In this case, the area the frame1 is sitting in gets expanded to accomodate frame3 since frame3 is so much bigger. The workaround here is to specify the columnspan keyword to allow frame3 to span more than 1 column on the grid.

frame3.grid(row=1,column=0,columnspan=2)

Upvotes: 4

Related Questions