user5736915
user5736915

Reputation:

Python Tkinter: Cannot insert text entry box in frame

I cannot understand why my following tkinter script doesnt work properly. When I run it, I see it compiles with no errors, but I dont actually see any window open. The error occurs when I try to pack the text entry field. Any help will be greatly appreciated.

from Tkinter import *
import ttk


main = Tk()
main.title('Testing')

topframe = Frame(main)
topframe.pack( side = TOP)

bottomframe = Frame(main)
bottomframe.pack( side = BOTTOM )


frame = Frame(main)
frame.pack()

Label(topframe, text="Date").grid(row=0)
e1 = Entry(topframe)
e1.insert(1, "May 24, 2017")
e1.pack()
main.mainloop()

Upvotes: 2

Views: 5635

Answers (2)

Mike - SMT
Mike - SMT

Reputation: 15236

Actually there is an error when trying to run something like this.

_tkinter.TclError: cannot use geometry manager pack inside .!frame which already has slaves managed by grid

You need to take care as to how you use your layout managers like .pack, .grid, .place. Check out this link For info on the geometry manager.

change:

Label(topframe, text="Date").grid(row=0)

To:

Label(topframe, text="Date").pack()

And your program will run fine.

This works fine for me. Note at this point you don't need to import ttk

from Tkinter import *

main = Tk()
main.title('Testing')

topframe = Frame(main)
topframe.pack( side = TOP)

bottomframe = Frame(main)
bottomframe.pack( side = BOTTOM )

frame = Frame(main)
frame.pack()

Label(topframe, text="Date").pack()
e1 = Entry(topframe)
e1.insert(1, "May 24, 2017")
e1.pack()

main.mainloop()

Results in:

enter image description here

Upvotes: 2

jasonharper
jasonharper

Reputation: 9621

All children of any one widget have to use the same geometry manager. Specifically, your topframe has a Label using .grid(), and an Entry using .pack(); the two geometry managers will never be able to agree on a positioning of the widgets. Change one of them to match the other, it's your choice which one to change (the fact that topframe was packed into its parent does not affect your choice here).

Upvotes: 1

Related Questions