Etchosh
Etchosh

Reputation: 109

Adjusting location of Entry in Tkinter

I am trying to get the text username and password in the below Tkinter to be next to the Entry, I have tried to play with the columns but it didn't fix it. I was previously using pack() but switched to grid() to be more in control of the location of the labels.

Here is the code:

root = Tk()
root.title("Bookmark Search")
root.resizable(0,0)

greeting= Label(root, text="Hi, This is a Trial to see how the label works !")
help=Label(root, text=" How can I help you today?")
greeting.grid(row = 0, column= 1, pady=5, padx=200)
help.grid(row = 1, column= 1,pady=5)


e = Entry(root, width=50)
e.grid(row=2, column = 1,rowspan=2, pady=15)
mySubmit = Label(root)

-------several lines of unrelated code-------

mySubmit.bind("<Button-1>", open_url)

root.bind('<Return>', myClick)

myButton= Button(root, text="Submit", command=myClick)
myButton.grid(row=4, column = 1,rowspan=2, pady=10)

# username
username_label = Label(root, text="Username:")
username_label.grid(column=0, row=8, padx=5, pady=5)

username_entry = Entry(root)
username_entry.grid(column=1, row=8, padx=5, pady=5)

# password
password_label = Label(root, text="Password:")
password_label.grid(column=0, row=9, padx=5, pady=5)

password_entry = Entry(root)
password_entry.grid(column=1, row=9, padx=5, pady=5)

# login button
login_button = Button(root, text="Login")
login_button.grid(column=1, row=10,padx=5, pady=5)
root.mainloop()

Here is a print screen of the current output: enter image description here

Here is the required output: enter image description here

Upvotes: 0

Views: 480

Answers (1)

furas
furas

Reputation: 142651

You can put Labels and Buttons in Frame and then Frame put in main window.


import tkinter as tk  # PEP8: `import *` is not preferred

# --- functions --- (PEP8: lower_case_names)

def open_url():
    pass

def my_click():
    pass

# --- main ---

root = tk.Tk()
root.title("Bookmark Search")
root.resizable(0,0)

greeting = tk.Label(root, text="Hi, This is a Trial to see how the label works !")
help = tk.Label(root, text=" How can I help you today?")

greeting.grid(row=0, column=1, pady=5, padx=200)
help.grid(row=1, column=1, pady=5)

e = tk.Entry(root, width=50)
e.grid(row=2, column=1, pady=15)

my_submit = tk.Label(root, text='LABEL')
my_submit.grid(row=3, column=1, pady=10)
my_submit.bind("<Button-1>", open_url)

root.bind('<Return>', my_click)

my_button = tk.Button(root, text="Submit", command=my_click)
my_button.grid(row=4, column=1, pady=10)

# - start Frame - to group widgets -

frame = tk.Frame(root)
frame.grid(column=1, row=5)

# username
username_label = tk.Label(frame, text="Username:")
username_label.grid(column=0, row=8, padx=5, pady=5)

username_entry = tk.Entry(frame)
username_entry.grid(column=1, row=8, padx=5, pady=5)

# password
password_label = tk.Label(frame, text="Password:")
password_label.grid(column=0, row=9, padx=5, pady=5)

password_entry = tk.Entry(frame)
password_entry.grid(column=1, row=9, padx=5, pady=5)

# - end Frame -

# login button
login_button = tk.Button(root, text="Login")
login_button.grid(column=1, row=10, padx=5, pady=5)

root.mainloop()

PEP 8 -- Style Guide for Python Code

Upvotes: 3

Related Questions