'How can I shift my Entry and Label widget to the right and align them in Tkinter?
I am making a program on Tkinter,python and I want to move 3 label and entry widgets to the right so that they are in line and one below the other. I am using grid but its very frustrating because the column is not moving further than 6. Here's the code:
from tkinter import *
from tkinter import ttk
from tkinter import Text
import tkinter as tk
win = tk.Tk()
win.geometry("1200x988")
def display_text():
global entry
string = entry.get()
label.configure(text=string)
entry = Entry(win, width=40)
entry.focus_set()
label = Label(win, text="Name:", font=("Courier 22 bold"))
label.grid(row=0, column=0, sticky=W, pady=2)
entry.grid(row=1, column=0, pady=2)
label2 = Label(win, text="Number:", font=("Courier 22 bold"))
label2.grid(row=2, column=0, sticky=W, pady=2)
entry2 = Entry(win, width=40)
entry2.focus_set()
entry2.grid(row=3, column=0, sticky=W, pady=2)
label3 = Label(win, text="Address:", font=("Courier 22 bold"))
label3.grid(row=4, column=0, sticky=W, pady=2)
entry3 = tk.Entry(win, width=60)
entry3.grid(row=5, column=0, sticky=W, ipady=20)
ttk.Button(win, text="Okay", width=20,
command=display_text).grid(row=6, column=0, pady=2)
win.mainloop()
Solution 1:[1]
Use rows and colums:
from tkinter import ttk
import os
import tkinter as tk
win = tk.Tk()
win.wm_title("Data entry")
def display_text():
string = entry.get()
if string:
label.configure(text=string)
label = tk.Label(win, text="Name:", font=("Courier 22 bold"))
label.grid(row=0, column=0, sticky=tk.W, pady=2)
entry = tk.Entry(win, width=40)
entry.focus_set()
entry.grid(row=0, column=1, sticky=tk.EW, pady=2)
label2 = tk.Label(win, text="Number:", font=("Courier 22 bold"))
label2.grid(row=1, column=0, sticky=tk.W, pady=2)
entry2 = tk.Entry(win, width=40)
entry2.grid(row=1, column=1, sticky=tk.EW, pady=2)
label3 = tk.Label(win, text="Address:", font=("Courier 22 bold"))
label3.grid(row=2, column=0, sticky=tk.W, pady=2)
entry3 = tk.Entry(win, width=60)
entry3.grid(row=2, column=1, sticky=tk.EW, ipady=20)
ttk.Button(win, text="Okay", command=display_text).grid(
row=3, column=0, pady=2
)
win.mainloop()
The window will look like this:
I've left out the title bar on purpose; this looks different depending on the OS you use.Some additional points:
- Using
from tkinter import *is considered bad style these days. - Setting the geometry is not really necessary.
- Don't forget to set the window title with
wm_title. - Making the
Entrywidgets sticky on ease and west (tk.EW) makes them fill the column. - Not prescribing the width of the button makes column 0 slimmer.
- Personally, I'd rather have all the text the same size, so I would set the font globally:
import tkinter.font as tkfont
win = tk.Tk()
win.wm_title("Data entry")
default_font = tkfont.nametofont("TkDefaultFont")
default_font.configure(
family="Courier",
size=22,
weight="bold"
)
win.option_add("*Font", default_font)
(I've left out the title bar on purpose; this looks different depending on the OS you use.)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

