Rebecca Procter
Rebecca Procter

Reputation: 1

Spacing using Tkinter in Python

I'm experimenting with Tkinter for the first time. I'm writing an interface for a caesar cipher program. How can I put the second label and text box underneath the first? I tried using \n but that only put the label underneath, not the textbox.

from tkinter import *

top=Tk()

text= Text(top)
text.insert(INSERT, "This is a Caesar Cipher encrypter.")
L1 = Label(top, text="Enter your text here")
L1.pack( side = LEFT)
E1 = Entry(top, bd =5)
E1.pack(side = RIGHT)

L2 = Label(top, text="Enter your key here")
L2.pack( side = LEFT)
E2 = Entry(top, bd =5)
E2.pack(side = RIGHT)

top.mainloop()

Gives me: The resulting window

How would you suggest I fix this?

Upvotes: 0

Views: 179

Answers (1)

Miraj50
Miraj50

Reputation: 4407

I would recommend you to use the Grid Geometry manager instead of pack here as you have much finer control over the placement of your widgets.

from tkinter import *

top=Tk()

text= Text(top)
text.insert(INSERT, "This is a Caesar Cipher encrypter.")
L1 = Label(top, text="Enter your text here")
L1.grid(row=0, column=0)
E1 = Entry(top, bd =5)
E1.grid(row=0, column=1)

L2 = Label(top, text="Enter your key here")
L2.grid(row=1, column=0)
E2 = Entry(top, bd =5)
E2.grid(row=1, column=1)

top.mainloop()

enter image description here

Upvotes: 2

Related Questions