I am creating a tkinter grid with 10 columns and 5 rows, including an input textbox. I need to know how to create a fixed header and assign a title to the header. Any suggestions would be appreciated
from tkinter import *
root = Tk()
frame = Frame(root)
root.rowconfigure(0, weight=5)
root.columnconfigure(0, weight=10)
frame.grid(row=0, column=0, sticky="news")
grid = Frame(frame)
grid.grid(sticky="news", column=0, row=7, columnspan=2)
frame.rowconfigure(7, weight=1)
frame.columnconfigure(0, weight=1)
grid.Label(root, text='Title-1').grid(row=0, column=0)
grid.Label(root, text='Title-2').grid(row=0, column=1)
grid.Label(root, text='Title-3').grid(row=0, column=2)
grid.Label(root, text='Title-4').grid(row=0, column=3)
grid.Label(root, text='Title-5').grid(row=0, column=4)
grid.Label(root, text='Title-6').grid(row=0, column=5)
grid.Label(root, text='Title-7').grid(row=0, column=6)
grid.Label(root, text='Title-8').grid(row=0, column=7)
grid.Label(root, text='Title-9').grid(row=0, column=8)
grid.Label(root, text='Title-10').grid(row=0, column=9)
x=1
#example values
for x in range(10):
for y in range(5):
text = Entry(frame)
text.grid(column=x, row=y, sticky="news")
frame.columnconfigure(tuple(range(10)), weight=1)
frame.rowconfigure(tuple(range(5)), weight=1)
root.mainloop()
grid.Label(root, text='Title-1').grid(row=0, column=0)
1
Here’s a minimal example that I believe accomplishes what you’re looking for.
I’ll leave figuring out how to reference the Entry
widgets to you since that wasn’t mentioned.
A grid of Entry
widgets gets inserted into a Frame
along with a row of title Label
s whose text (and quantity) is determined by the items in the titles
list.
The number of columns is determined by the number of title strings in the titles
list.
import tkinter as tk
root = tk.Tk()
frame = tk.Frame(root)
frame.pack()
titles = [
'Title-1',
'Title-2',
'Title-3',
'Title-4',
'Title-5',
'Title-6',
'Title-7',
'Title-8',
'Title-9',
'Title-10',
]
for column, title in enumerate(titles):
# add column header labels
tk.Label(frame, text=title).grid(row=0, column=column)
for row in range(5): # add 5 rows of Entry widgets
text = tk.Entry(frame)
# row here is offset by +1 because the titles are using row 0
text.grid(column=column, row=row + 1)
root.mainloop()