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
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:
In this case, the area the
frame1is sitting in gets expanded to accomodateframe3sinceframe3is so much bigger. The workaround here is to specify thecolumnspankeyword to allowframe3to span more than 1 column on the grid.