Code in Python
Machine Learning and Deep Learning Blogs
search

Working with Grid Layout in Tkinter

In this chapter, we are going to organize the widgets in the main window in a grid layout. Previously, we used pack(), which just places them on the screen. We could just change the position of the widget i.e., we could place them one over other or side by side. But it didn’t give you a lot of control to say exactly where you want to pack it in.

In grid layout, we can line up our rows and columns. We can specify the exact row and column in which the widget should come.

Now, let’s make 2 labels and 2 entry widgets because we need the stuff to organize. It will be kind of a login screen where we enter name and password.

name = Label(var, text = "name")
password = Label(var, text = "password")

Now, whenever you want to allow the user to enter a simple number or name or password or anything we have to use something called entry. It is the same thing as an input in web design. It is a blank single line of a text form.

entry1 = Entry(var)
entry2 = Entry(var)

We need one for the name and one for a password. Now, we have 2 text prompts and 2 input fields.

To organize it, think of this as an excel sheet. We have rows that go left and right and column that goes up and down. By default, if we don’t specify one, it is going to assume the first. If we put just row = 2, its gonna assume it as the first column.

In order to place anything, just write where do you want to place this label and how do you want to place it.
Both the labels will go like this,

name.grid(row=0)
password.grid(row=1) 

Now, to position those entries, we will write,

entry1.grid(row=0, column=1)
entry2.grid(row=1, column=1) 

In the above window, the name and password are center aligned, we need them to be right aligned.
To do that, we have already placed the label in the window. Now, whenever we want to align it to either right, left, top or bottom, we will use a new parameter called sticky. If we want to place it to the right, we will write sticky = E.

Whenever we are using the grid, it’s not going to take left, right, top or bottom. It’s gonna take N E W S. North on the top, East to right, West on the left and South on the bottom.
So the labels will be now,

name.grid(row=0, sticky = E)
password.grid(row=1, sticky = E) 

If we are making a login for the website, they have a checkbox called ‘keep me logged in’. Making a checkbox is real simple.
To make a checkbox, use a parameter Check button and in it, specify the parent window or the frame you want to put it in. then write the text you want it to show. For eg., ‘keep me logged in’.
Now we want to place it in the main window. Now, to place the checkbox in the center of both the label and entry box (putting it into 2 columns together), we need to write columnspan = 2 in place of a column. This will combine 2 columns and treat it as one column.

c = Checkbutton(var, text = "keep me logged in")
c.grid(row=2, columnspan=2) 

Final Code

the whole program combined will look like,

from Tkinter import *
var = Tk()
name = Label(var, text = "name")
password = Label(var, text = "password")
entry1 = Entry(var)
entry2 = Entry(var)
name.grid(row=0, sticky = E)
password.grid(row=1, sticky = E)
entry1.grid(row=0, column=1)
entry2.grid(row=1, column=1)
c = Checkbutton(var, text = "keep me logged in")
c.grid(row=2, columnspan=2)
var.mainloop() 

grid layout

Rahul Choudhary
Follow me

Rahul Choudhary

Rahul is a Python enthusiast. He likes to explore whatever is new in Python world. Rahul is a car fanatic and a serial chiller. Follow him on Facebook and Twitter.
Rahul Choudhary
Follow me

Latest posts by Rahul Choudhary (see all)

13 Comments

  1. I think the admin of this web site is truly working hard for his site, as here every material is quality based stuff.

  2. Hi my loved one! I want to say that this article is awesome, great written and include approximately all important infos.

    I would like to look extra posts like this
    .

  3. Thanks in support of sharing such a nice opinion, piece of writing is good, thats why i have read it fully

  4. What’s up friends, good post and good arguments commented here,
    I am truly enjoying by these.

  5. You really make it appear really easy together with your presentation however I to
    find this matter to be really something that I believe I
    would never understand. It seems too complicated and very broad for me.
    I’m having a look forward to your subsequent publish, I’ll try to get the grasp of it!

  6. I enjoy your work, regards for all the informative posts.

  7. You ought to be a part of a contest for one of the most useful sites online. I’m going to highly recommend this site!

  8. I like it when individuals come together and share ideas.
    Great blog, stick with it!

  9. This piece of writing will assist the internet visitors for building up new weblog or even a weblog from start to end.

  10. We’re a group of volunteers and opening a new scheme in our community.
    Your website provided us with valuable information to work
    on. You have done an impressive job and our entire community
    will be thankful to you.

Leave a Reply

Your email address will not be published.

*

© 2018 Code in Python