Cody Pritchard
Cody Pritchard

Reputation: 695

Python Tkinter not opening when using .grid()

I am working through online tutorials to learn how to make a GUI with Tkinter. But i am having an issue when using the .grid() function.

Here is what i have so far:

from Tkinter import *

root = Tk()
title = Label(root, text = "Hello World")
title.pack()

name           = Label(root, text = "Name")
password       = Label(root, text = "Password")
entry_name     = Entry(root)
entry_password = Entry(root)

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

check = Checkbutton(root, text = "Keep me logged in")
check.grid(columnspan = 2)

root.mainloop()

So the problem im having is as soon as i include that first line:

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

And then run the script, there are no errors, but nothing opens. The program just hangs and i have to close the command prompt in order to regain control.

If i comment out all the lines using .grid() they program works fine and will open a window with my title inside it.

So, does anyone know what im doing wrong? Im using Python 2.7

Upvotes: 1

Views: 2162

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 386010

You cannot use both pack and grid with two or more widgets that share the same parent. In your case you're using pack for title and grid for everything else. Use one or the other.

Upvotes: 5

sw123456
sw123456

Reputation: 3459

Keep grid geometry manager consistent so use title.grid().

from tkinter import *

root = Tk()
title = Label(root, text = "Hello World")
title.grid()

name           = Label(root, text = "Name")
password       = Label(root, text = "Password")
entry_name     = Entry(root)
entry_password = Entry(root)

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

check = Checkbutton(root, text = "Keep me logged in")
check.grid(columnspan = 2)

root.mainloop()

Upvotes: 4

Related Questions