简体   繁体   中英

Is there a way to center both a label and entry box on a specific row in tkinter?

I am trying to design a screen in Python (Tkinter) and after researching the issue completely, I was not able to find a way to center both the Label and Entry box on the screen in the row that I would like. To be clear, I would not like it in the center of the screen, but rather in the center of the row I have selected.

I have tried some methods of .pack() and using a grid to do it but nothing seems to do what I would like.

I set the root like this:

root = tk.Tk()

I set the GUI width and height like this:

screen_width = str(root.winfo_screenwidth())
screen_height = str(root.winfo_screenheight())
root.geometry(screen_width + "x" + screen_height)

And I set the positions of the labels and their entry boxes like this:

fName = tk.Label(root, text="First Name")
fName.grid(row=0)
lName = tk.Label(root, text="Last Name")
lName.grid(row=1)
ageLabel = tk.Label(root, text="Age")
ageLabel.grid(row=2)
correctedLabel = tk.Label(root, text="Is your vision, or corrected to, 20/20? (Y/N)")
correctedLabel.grid(row=3)
genderLabel = tk.Label(root, text="Gender")
genderLabel.grid(row=4)

e1 = tk.Entry(root)
e2 = tk.Entry(root)
e3 = tk.Entry(root)
e4 = tk.Entry(root)
e5 = tk.Entry(root)
root.winfo_toplevel().title("Information Collection")


e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1)
e4.grid(row=3, column=1)
e5.grid(row=4, column=1)

With the current code I have, It will take the screen width and height from Tkinter and will resize the window to the screen size. Also using this code someone would see that there are 4 labels, and their corresponding entry boxes, and I would like to move each group of label and its entry to the center of its row. I would appreciate any help I could get on this.

You can start by setting a weight of your grid , which adjusts the weight of each row/column should occupy:

root.grid_columnconfigure(0,weight=1)
root.grid_columnconfigure(1,weight=1)

Now you should see both the left/right labels are evenly spread throughout your screen, which is what you intended. If you somehow want to make them properly centered, you can apply a sticky direction to the widgets.

fName.grid(row=0,sticky="e")
...
e1.grid(row=0, column=1,sticky="w")
...

Full sample:

import tkinter as tk

root = tk.Tk()
root.title("Information Collection")
root.geometry(f"{root.winfo_screenwidth()}x{root.winfo_screenheight()}")

labels = ("First Name","Last Name","Age","Is your vision, or corrected to, 20/20? (Y/N)","Gender")
entries = []
for num, i in enumerate(labels):
    l = tk.Label(root, text=i)
    l.grid(row=num, column=0, sticky="e") #remove sticky if not required
    e = tk.Entry(root)
    e.grid(row=num, column=1, sticky="w") #remove sticky if not required
    entries.append(e) #keep the entries in a list so you can retrieve the values later

root.grid_columnconfigure(0,weight=1)
root.grid_columnconfigure(1,weight=1)

root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM