Tkinter: Why is the next 'Label' taking it's 0 column differently? Tkinter: Why is the next 'Label' taking it's 0 column differently? tkinter tkinter

Tkinter: Why is the next 'Label' taking it's 0 column differently?


Use Sticky

import tkinter as tkwindow =tk.Tk()label = tk.Label(window, text="Guess a number that can be anything from '10' to '50', you have 5 chances !!!", fg="black", bg="white", font=("Arial Bold", 25))label.grid(row=0)inp = tk.Entry(window)inp.grid(row=1,sticky="w")window.mainloop()


Column zero has a very wide label in it which forces the column to be as wide as the label. The entry widget is in that same very wide column, and by default grid centers items in the column.

If you want the entry widget aligned to the left, you can use the sticky option to force the widget to "stick" to the west (left) side of the column.

inp.grid(row=1,sticky="w")

If these are the only two widgets in the UI, this is probably the right solution. If you plan to have other widgets aligned with either the label or the entry widget, you may need to do something else. Without knowing more about the final product it's hard to say for sure.

Another solution would be to configure the label to span two columns, and have the second column be given all of the extra space. That will cause the first column to be as small as necessary.

label.grid(row=0, columnspan=2)window.grid_columnconfigure(1, weight=1)

The choice depends a bit on what else you plan to do with the window. If you're only going to have these two widgets and nothing else, the first solution requires one less line of code. The second solution makes it easier to add other widgets on the same row as the entry widget.