Home > Back-end >  How do I stop text from moving when I resize the screen in tkinter
How do I stop text from moving when I resize the screen in tkinter

Time:10-22

I am creating a registration form, and I have coded labels to show next to the text box for someone's username and password. This is the code I am using to place the text boxes and labels:

usernamebx.place(relx=0.5, rely=0.5, width=225, height=25, 
anchor= CENTER)

userbx_label.place(relx=0.1, rely=0.5, anchor=CENTER)

passwbx.place(relx=0.5, rely=0.6, width=225, height=25, anchor = CENTER)

passwbx_label.place(relx=0.1, rely=0.6, anchor=CENTER)

The code for usernamebx and passwbx means that the text boxes don't move when I resize the tkinter window. However, I have done the same with the labels for each but it doesn't work. Any help?

CodePudding user response:

The code for usernamebx and passwbx means that the text boxes don't move when I resize the tkinter window.

Actually, they do move! If you put a widget at relx 0.5 in a window that is 200 pixels wide, that means the center of the widget will be 100 pixels from the left edge of the window. When you grow the window to 400 pixels wide, the center of the widget now will be 200 pixels from the left edge. It moved 100 pixels. You don't see it because it's symmetrical so it stays in the center.

The same happens with a widget that is at 0.1. on a 200 pixel wide window it's going to be 20 pixels from the left edge. When you make the window 400 pixels widget it's going to be 40 pixels from the edge.

This is the nature of relative coordinates -- they will always change when the window is resized.

It's hard to see what your actual requirement is, though I'm guessing you want the username label entry and password label entry to be co-aligned in the center of the window.

If that's the case, one simple solution is to put those widgets in a frame. Use grid internally since it appears that you are in fact creating a grid. Then, you can place the frame in the window as a separate step.

Here's an example of the technique. For illustrative purposes the frame has a visible border, but that's not strictly necessary. You can remove the border to make it blend in with the background.

This example uses place to put the frame in the center, though you can also use pack.

import tkinter as tk

root = tk.Tk()
root.geometry("400x400")
inner_frame = tk.Frame(root, bd=2, relief="groove")

usernamebx = tk.Entry(inner_frame)
userbx_label = tk.Label(inner_frame, text="Username:")
passwbx = tk.Entry(inner_frame)
passwbx_label = tk.Label(inner_frame, text="Password:")

inner_frame.grid_columnconfigure(0, weight=1)

userbx_label.grid(row=0, column=0, sticky="e")
usernamebx.grid(row=0,column=1, sticky="ew")
passwbx_label.grid(row=1, column=0, sticky="e")
passwbx.grid(row=1, column=1, sticky="ew")

inner_frame.place(relx=.5, rely=.5, anchor="center")

root.mainloop()

If you want to use pack rather than place, have the packer expand the allocated space to be the whole window, and the frame will automatically be centered. In this case the window will shrink to fit the frame plus the padding.

inner_frame.pack(side="top", expand=True, padx=10, pady=10)

screenshot

  • Related